Go is in development for v1. Interested in contributing or chatting with us?Get in touch!
Go - Websocket.On()
Register a handler for connections, disconnections, or messages for the websocket.
import (
"fmt"
"github.com/nitrictech/go-sdk/faas"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
ws, err := nitric.NewWebsocket("public")
if err != nil {
return
}
ws.On(faas.WebsocketConnect, func(ctx *faas.WebsocketContext, next faas.WebsocketHandler) (*faas.WebsocketContext, error) {
// handle connections
return next(ctx)
})
ws.On(faas.WebsocketDisconnect, func(ctx *faas.WebsocketContext, next faas.WebsocketHandler) (*faas.WebsocketContext, error) {
// handle disconnections
return next(ctx)
})
ws.On(faas.WebsocketMessage, func(ctx *faas.WebsocketContext, next faas.WebsocketHandler) (*faas.WebsocketContext, error) {
// handle messages
return next(ctx)
})
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Parameters
- Name
eventType
- Required
- Required
- Type
- WebsocketEventType
- Description
The type of websocket event to listen for. Can be
WebsocketConnect
,WebsocketDisconnect
, orWebsocketMessage
.
- Name
middleware
- Required
- Required
- Type
- WebsocketMiddleware
- Description
The middleware function to use as the handler for Websocket events. If you want to compose more than one middleware use
faas.ComposeWebsocketMiddleware
.
Examples
Register a handler for message events
ws.On(faas.WebsocketMessage, func(ctx *faas.WebsocketContext, next faas.WebsocketHandler) (*faas.WebsocketContext, error) {
message := string(ctx.Request.Data())
fmt.Printf("New Message from %s: %s\n", ctx.Request.ConnectionID(), message)
return next(ctx)
})
Manage Websocket connections
To store Websocket connections you can use a Nitric collection.
import (
"fmt"
"github.com/nitrictech/go-sdk/faas"
"github.com/nitrictech/go-sdk/nitric"
)
func main() {
ws, err := nitric.NewWebsocket("public")
if err != nil {
return
}
connections, err := nitric.NewCollection("connections").With(nitric.CollectionEverything...)
if err != nil {
return
}
ws.On(faas.WebsocketConnect, func(ctx *faas.WebsocketContext, next faas.WebsocketHandler) (*faas.WebsocketContext, error) {
err := connections.Doc(ctx.Request.ConnectionID()).Set(ctx.Request.Context(), map[string]interface{}{
// store any metadata related to the connection here
"connectionId": ctx.Request.ConnectionID(),
})
if err != nil {
return ctx, err
}
return next(ctx)
})
ws.On(faas.WebsocketDisconnect, func(ctx *faas.WebsocketContext, next faas.WebsocketHandler) (*faas.WebsocketContext, error) {
// remove the connection from the collection
err := connections.Doc(ctx.Request.ConnectionID()).Delete(ctx.Request.Context())
if err != nil {
return ctx, err
}
return next(ctx)
})
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}
Chain functions as a single method handler
import (
"fmt"
"github.com/nitrictech/go-sdk/faas"
"github.com/nitrictech/go-sdk/nitric"
)
func validateMessage(ctx *faas.WebsocketContext, next faas.WebsocketHandler) (*faas.WebsocketContext, error) {
// validate that the message does not contain explicit content
return next(ctx)
}
func handleMessage(ctx *faas.WebsocketContext, next faas.WebsocketHandler) (*faas.WebsocketContext, error) {
message := string(ctx.Request.Data())
fmt.Printf("New Message from %s: %s\n", ctx.Request.ConnectionID(), message)
return next(ctx)
}
func main() {
ws, err := nitric.NewWebsocket("public")
if err != nil {
return
}
ws.On(faas.WebsocketMessage, faas.ComposeWebsocketMiddleware(validateMessage, handleMessage))
if err := nitric.Run(); err != nil {
fmt.Println(err)
}
}