Advanced

Websocket Endpoint

Edit this page

Websocket endpoint may be included by passing the ws handler file you specify in your start config. Note that this feature is experimental on the Nitro server and its config may change in future releases of SolidStart. Use it with caution.

import { defineConfig } from "@solidjs/start/config";
export default defineConfig({
server: {
experimental: {
websocket: true,
},
},
}).addRouter({
name: "party",
type: "http",
handler: "./src/ws.ts",
target: "server",
base: "/ws",
});

Inside the ws file, you can export a partyHandler function to manage WebSocket connections and events:

import { partyHandler } from "vinxi/party";
export default partyHandler({
onConnect(party, conn) {
conn.send(`Hello from the server!: ${conn.id}`);
},
onMessage(party, msg, con) {
con.send(`${party.id} receive: ${msg}`);
},
});

The partyHandler supports 6 event callbacks:

  • onStart: Invoked when the party room is firstly created.
  • onConnect: Invoked when a new connection is established.
  • onMessage: Invoked when a message is received.
  • onClose: Invoked when a connection is closed.
  • onError: Invoked when an error occurs.
  • onRequest: Invoked on a HTTP request is made to the party URL.

The party.id represents the URL path that is shared between all connections in the party.

Beside partyHandler you can also use eventHandler to have a more precise control.

Report an issue with this page