Low-Level WebSocket Handler
Actors can handle WebSocket connections through the onWebSocket handler.
For most use cases, actions and events provide high-level connection handling powered by WebSockets that's easier to work with than low-level WebSockets. However, low-level handlers are required when implementing custom use cases.
Handling WebSocket Connections
The onWebSocket handler manages low-level WebSocket connections. It receives the actor context and a WebSocket object.
See also the raw WebSocket handler example.
Connecting To Actors
Via RivetKit Client
Use the .websocket() method on an actor handle to open a WebSocket connection to the actor's onWebSocket handler. This can be executed from either your frontend or backend.
The .websocket() method returns a standard WebSocket.
Via HTTP API
This handler can be accessed with raw WebSockets using wss://api.rivet.dev/gateway/{actorId}@{token}/websocket/{...path}.
For example, to connect to the chat actor above:
The path after /websocket/ is passed to your onWebSocket handler and can be used to route to different functionality within your actor. For example, to connect with a custom path /admin:
See the HTTP API reference for more information on WebSocket routing and authentication.
Via Proxying Connections
You can proxy WebSocket connections from your own server to actor handlers using the RivetKit client. This is useful when you need to add custom authentication or connection management before forwarding to actors.
See also the raw WebSocket handler with proxy example.
Connection & Lifecycle Hooks
onWebSocket will trigger the onBeforeConnect, onConnect, and onDisconnect hooks. Read more about lifecycle hooks.
Open WebSockets will be listed in c.conns. conn.send and c.broadcast have no effect on low-level WebSocket connections. Read more about connections.
WinterTC Compliance
The onWebSocket handler uses standard WebSocket APIs and will work with existing libraries expecting WinterTC-compliant WebSocket objects.
Advanced
WebSocket Hibernation
WebSocket hibernation allows actors to go to sleep while keeping WebSocket connections alive. Actors automatically wake up when a message is received or the connection closes.
Enable hibernation by setting canHibernateWebSocket: true. You can also pass a function (request) => boolean for conditional control.
Since open only fires once when the client first connects, use c.conn.state to store per-connection data that persists across sleep cycles. See connections for more details.
Accessing the Request
The underlying HTTP request is available via c.request. This is useful for accessing the path or query parameters.
Async Handlers
The onWebSocket handler can be async, allowing you to perform async code before setting up event listeners:
API Reference
WebSocketContext- Context for WebSocket handlersUniversalWebSocket- Universal WebSocket interfacehandleRawWebSocketHandler- Function to handle raw WebSocketUpgradeWebSocketArgs- Arguments for WebSocket upgrade