lustre_websocket

Package Version Hex Docs

Use websockets from your lustre application!

Quick start

Add to your Gleam project:

gleam add lustre_websocket

Typical usage looks like

import lustre_websocket as ws

pub type Msg {
  WsWrapper(ws.WebSocketEvent)
}

let init = #(Model(None), ws.init("/path", WsWrapper))

and then you pass init as first argument to lustre.application. But you can create a socket at any time, esp. re-create it after it is closed by the server.

The events can be handled like this:

update(model, msg) {
  case msg {
    WsWrapper(OnOpen(socket)) -> #(Model(..model, ws: Some(socket)), ws.send(socket, "client-init"))
    WsWrapper(OnMessage(msg)) -> todo
    WsWrapper(OnClose(reason)) -> #(Model(..model, ws: None), cmd.none())
  }
}

which also demonstrates how you send a text message over the socket.

Caveat

This package cannot handle more than 1 socket on a server endpoint

TODO:

Search Document