Go - Websocket.On()

Register a handler for connections, disconnections, or messages for the websocket.

import (
  "github.com/nitrictech/go-sdk/nitric"
  "github.com/nitrictech/go-sdk/nitric/websockets"
)

func main() {
  ws := nitric.NewWebsocket("public")

  ws.On(websockets.EventType_Connect, func(ctx *websockets.Ctx) {
    // handle connections
  })

  ws.On(websockets.EventType_Disconnect, func(ctx *websockets.Ctx) {
    // handle disconnections
  })

  ws.On(websockets.EventType_Message, func(ctx *websockets.Ctx) {
    // handle messages
  })

  nitric.Run()
}

Parameters

  • Name
    eventType
    Required
    Required
    Type
    WebsocketEventType
    Description

    The type of websocket event to listen for. Can be EventType_Connect, EventType_Disconnect, or EventType_Message.

  • Name
    handler
    Required
    Required
    Type
    interface{}
    Description

    The callback function to use as the handler for Websocket events.

Examples

Register a handler for message events

ws.On(websockets.EventType_Message, func(ctx *websockets.Ctx) {
  fmt.Printf("New Message from %s: %s\n", ctx.Request.ConnectionID(), ctx.Request.Message())
})

Manage Websocket connections

To store Websocket connections you can use a Nitric collection.

import (
  "context"

  "github.com/nitrictech/go-sdk/nitric"
  "github.com/nitrictech/go-sdk/nitric/keyvalue"
  "github.com/nitrictech/go-sdk/nitric/websockets"
)

func main() {
  ws := nitric.NewWebsocket("public")

  connections := nitric.NewKv("connections").Allow(keyvalue.KvStoreGet, keyvalue.KvStoreSet, keyvalue.KvStoreDelete)

  // Register a new connection on connect
  ws.On(websockets.EventType_Connect, func(ctx *websockets.Ctx) error {
    return connections.Set(context.TODO(), ctx.Request.ConnectionID(), map[string]interface{}{
      "connectionId": ctx.Request.ConnectionID(),
    })
  })

  // Remove a registered connection on disconnect
  ws.On(websockets.EventType_Disconnect, func(ctx *websockets.Ctx) error {
    return connections.Delete(context.TODO(), ctx.Request.ConnectionID())
  })

  // Broadcast message to all the registered websocket connections
  ws.On(websockets.EventType_Message, func(ctx *websockets.Ctx) error {
    connectionStream, err := connections.Keys(context.TODO())
    if err != nil {
      return err
    }

    for {
      connectionId, err := connectionStream.Recv()
      if err != nil {
        break // reached the end of the documents
      }

      err = ws.Send(context.TODO(), connectionId, []byte(ctx.Request.Message()))
      if err != nil {
        return err
      }
    }

    return nil
  })

  nitric.Run()
}