For WebSockets support in Golang, many examples feature Fiber servers with endpoints like this:
app.Use("/ws", func(c *fiber.Ctx) error {
// IsWebSocketUpgrade returns true if the client
// requested upgrade to the WebSocket protocol.
if websocket.IsWebSocketUpgrade(c) {
return c.Next()
}
return fiber.ErrUpgradeRequired
})
// Websocket handler
app.Get("/ws/:id", websocket.New(func(ws *websocket.Conn) {
handler.MediaStreamHandler(ws)
}))
The app.Use
endpoint here is well-documented: it initiates the WebSockets handshake and upgrades the connection protocol from HTTP to WebSockets.
Less clear is the app.Get
endpoint. I understand the idiomatic point: it's where the receipt of real-time messaging over WebSockets takes place. But what is not clear is who calls app.Get
, and how is the WebSockets interface persisted across invocations of app.Use
and app.Get
?
I am guessing state attached to the WebSockets interface is persisted in the Fiber app
.
But how is app.Get
called? Is the WebSockets client expected to make a request to app.Get
so the Fiber server is cued to begin receipt of real-time messaging?