Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PartyKit #126

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@
"use-debounce": "^10.0.1",
"usehooks-ts": "^3.1.0",
"vaul": "^0.9.1",
"zod": "^3.23.8"
"zod": "^3.23.8",
"partysocket": "1.0.2"
},
"devDependencies": {
"esbuild-register": "^3.5.0"
"esbuild-register": "^3.5.0",
"partykit": "0.0.111"
}
}
31 changes: 31 additions & 0 deletions apps/web/party/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type * as Party from "partykit/server";

export default class Server implements Party.Server {
constructor(readonly room: Party.Room) {}

onConnect(conn: Party.Connection, ctx: Party.ConnectionContext) {
// A websocket just connected!
console.log(
`Connected:
id: ${conn.id}
room: ${this.room.id}
url: ${new URL(ctx.request.url).pathname}`
);

// let's send a message to the connection
conn.send("hello from server");
}

onMessage(message: string, sender: Party.Connection) {
// let's log the message
console.log(`connection ${sender.id} sent message: ${message}`);
// as well as broadcast it to all the other connections in the room...
this.room.broadcast(
`${sender.id}: ${message}`,
// ...except for the connection it came from
[sender.id]
);
}
}

Server satisfies Party.Worker;
6 changes: 6 additions & 0 deletions apps/web/partykit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://www.partykit.io/schema.json",
"name": "web-party",
"main": "party/index.ts",
"compatibilityDate": "2024-10-17"
}
Loading
Loading