Home > database >  Communicate with NSQ over a Websocket
Communicate with NSQ over a Websocket

Time:07-28

Is it possible to communicate with NSQ over a websocket connection from a web app running in a user's browser?

Any built-in NSQ features or third-party library suggestions would be of a great help.

CodePudding user response:

As of recently there is the WSQ library that solves the task of communicating to NSQ over the websocket connection.

It allows to define message encoding/decoding logic on Websocket and NSQ sides separately. Also, WSQ supports injecting authentication and authorization logic, controlling user's access to subscribing and publishing to topics, and incoming message filtering based on message's content.

Creating WSQ server example:

// Create the default config
wsqConfig := wsq.NewConfig()
// Configure Websocket CheckOrigin callback to bypass any checks.
// Don't do it in production!
wsqConfig.SetWSCheckOrigin(wsq.CheckOriginBypass)

// Create the server instance specifying message and user types to use.
server := wsq.NewServer[message, *wsq.AnonymousUser](
    // Address (optional) and port to listen on
    ":9980",
    // WSQ Config instance
    wsqConfig,
    // NSQ Config instance
    nsq.NewConfig(),
    // WSQ Transformer struct providing encoders/decoders for NSQ and Websocket sides respectivly
    &wsq.Transformer[message]{NSQEnDec: &nsqEnDec{}, WSEnDec: &wsEnDec{}},
    // Authentication controller
    &wsq.NoAuthentication,
)
server.Run()
  • Related