Welcome to the party, pal!
This is a Partykit demo, showing how to:
- Reroute requests/connections to a different party by using an
onFetch
handler - Create secure per-user stores by implementing cookie/session authentication
https://onfetch.jevakallio.partykit.dev
This project contains two parties:
Exposes two API endpoints:
/login/:userId
— a fake login endpoint to set a cookie for given userId/user/*
— a router that forwards all requests to theuser.ts
party, using theuser
cookie value as the room id, i.e. every user will be routed to their separate room.
Simple PartyKit room that responds to HTTP request and WebSocket ping messages to demonstrate routing.
This room prevents direct access to the party's public URL, so the only access is via the onFetch
router via the main party.
The simplified version of the implementation is:
const userId = getCookie(req.headers.get("Cookie"), "user");
if (userId) {
return lobby.parties.user
.get(userId)
.fetch(req as unknown as RequestInit);
}
This gets the user cookie value, and then makes a fetch request to the user party with that akey as the id.
The client in client.ts
does the following:
- Calls the
/login/userId
endpoint to set the "user" cookie - Makes a HTTP POST request to
/user
with the cookie to demonstrate that request is routed to the user room (equivalent to/parties/user/:userId
). - Opens a WebSocket connection to
/user
with cookie to demonstrate that the websocket is routed to the user room (equivalent to/parties/user/:userId
).
Instead of the usual import PartySocket from "partysocket"
constructor, we use the raw reconnecting WebSocket
constructor, which takes a URL string as a parameter.
Note that for the cookie to be passed to the WebSocket, the WebSocket origin needs to be the same as the website host.
At the moment, until PartyKit gets custom domain support, this approach won't work for cross-origin requests unless you proxy the request via your own domain.
import { WebSocket } from "partysocket";
const host = document.location.host;
const protocol =
host.startsWith("localhost") || host.startsWith("127.0.0.1") ? "ws" : "wss";
const conn = new WebSocket(`${protocol}://${host}/user`, undefined, {
startClosed: true,
});