Incorrect message sending to Centrifuge

25 Views Asked by At

Please help me solve the error. I have a Docker container with Centrifuge. I need to send messages to the 'user' channel. Everything should happen on the backend side - without involving the browser. dokee-compose.yml

version: '3.7'
services:
centrifugo:
    container_name: centrifugo
    image: centrifugo/centrifugo:v3
    volumes:
      - ./dev_centrifugo_config.json:/centrifugo/config.json
    command: centrifugo -c config.json
    ports:
      - 8001:8000
    ulimits:
      nofile:
        soft: 65535
        hard: 65535

dev_centrifugo_config.config

{
    "token_hmac_secret_key": "my_secret",
    "api_key": "my_api_key",
    "admin_password": "password",
    "admin_secret": "secret",
    "admin": true,
    "allowed_origins": ["*"]
  }

function for sending messages

import (
    "context"
    "encoding/json"
    "errors"
    "fmt"
    "github.com/centrifugal/centrifuge-go"
    "github.com/gin-gonic/gin"
    "github.com/gorilla/websocket"
    "log"
    "time"
)
func sent() {
    url := "ws://localhost:8001/connection/websocket"
    apiKey := "my_api_key"

    headers := http.Header{}
    headers.Add("Authorization", "apikey "+apiKey)

    conn, _, err := websocket.DefaultDialer.Dial(url, headers)
    if err != nil {
        log.Fatal("error connecting to Centrifugo:", err)
    }
    defer conn.Close()

    message := map[string]interface{}{
        "method": "publish",
        "params": map[string]interface{}{
            "channel": "user",
            "message": "your_message",
        },
    }

    wrappedMessageJSON, err := json.Marshal(message)
    if err != nil {
        log.Fatal("error encoding wrapped message to JSON:", err)
    }

    err = conn.WriteMessage(websocket.TextMessage, wrappedMessageJSON)
    if err != nil {
        log.Fatal("error writing message to Centrifugo:", err)
    }
}

Get logs

centrifugo  | {"level":"info","client":"7b6128b9-e452-42b6-87f0-32aa8f2ae2db","data":"{\"method\":\"publish\",\"params\":{\"channel\":\"user\",\"message\":\"your_message\"}}","error":"json: cannot unmarshal \"\\\"publish\\\",\\\"params\\\":{\\\"channel\\\":\\\"u...\" into Go struct field protocol.Command.method of type int32","user":"","time":"2024-03-13T23:09:27Z","message":"error decoding command"}

What am I doing wrong and why can't I send a message to Centrifuge?

0

There are 0 best solutions below