Fetch and WebSocket Handler
Actors can handle HTTP requests and WebSocket connections through the onFetch
and onWebSocket
handlers.
For most use cases, actions and events provide high-level connection handling that's easier to work with. However, raw handlers are required when implementing custom use cases or integrating external libraries that need direct access to the underlying HTTP Request
/Response
objects or WebSocket connections.
Defining Handlers
onFetch(c, request, { auth })
The onFetch
handler processes HTTP requests sent to your actor. It receives the actor context and a standard Request
object.
WebSocket upgrades are not currently supported in onFetch
. Use onWebSocket
instead.
Also see the raw fetch handler example project.
onFetch
can be used to expose Server-Sent Events from Rivet Actors.
onWebSocket(c, websocket, { request, auth })
The onWebSocket
handler manages WebSocket connections. It receives the actor context, a WebSocket
object, and the initial Request
.
Also see the raw WebSocket handler with proxy example project.
Connection lifecycle hooks like onConnect
and onDisconnect
do not get called when opening WebSockets for onWebSocket
. This is because onWebSocket
provides a low-level connection. Use ws.addEventListener("open")
and ws.addEventListener("close")
instead.
Accessing Your Handlers
There are three ways to access your actor's fetch and WebSocket handlers:
Option A: From Backend via RivetKit Client
You can use the RivetKit client's built-in methods for raw HTTP and WebSocket access:
For more advanced use cases, you can forward requests to actor handlers from your server:
Option B: From Frontend with RivetKit Client
Use the RivetKit client to make direct HTTP requests or WebSocket connections:
Option C: From Frontend via Direct RivetKit Router Access
You can access your actor handlers directly through the mounted RivetKit router. The router automatically handles the required headers for authentication and routing.
For HTTP requests, the router expects these headers:
X-RivetKit-Actor-Query
: JSON-encoded actor queryX-RivetKit-Encoding
: Encoding type (usually "json")X-RivetKit-Conn-Params
: JSON-encoded connection parameters (optional)
Authentication
If you are using the external client, authentication is handled through the onAuth
handler. The onAuth
handler is executed on the server before the request is sent to the actor, reducing resource load on the actor by filtering out unauthorized requests early.
If you are using the server-side client, then authentication is skipped by default.
See the authentication documentation for detailed information on implementing authentication patterns.
State Saves
State changes in onFetch
and onWebSocket
handlers are automatically saved after the handler finishes executing.
For onWebSocket
handlers specifically, you'll need to manually save state using c.saveState()
while the WebSocket connection is open if you want state changes to be persisted immediately. This is because WebSocket connections can remain open for extended periods, and state changes made during event handlers (like message
events) won't be automatically saved until the connection closes.
For more details on state management, see State.
W3C Compliance
It's not possible to use the global fetch
method or global WebSocket class to connect to an actor. This is because actors do not have traditional network interfaces to communicate with.
However, the Request
, Response
, and WebSocket
types used with .fetch()
and .websocket()
comply with the W3C specification and will work wherever you pass them.