Skip to content

Commit

Permalink
partysub: POST a message to a partysub server
Browse files Browse the repository at this point in the history
  • Loading branch information
threepointone committed Aug 4, 2024
1 parent f1583be commit 1985f63
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-gorillas-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"partysub": patch
---

POST a message to a partysub server
16 changes: 16 additions & 0 deletions fixtures/pubsub/src/client/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from "react";
import { createRoot } from "react-dom/client";
import { nanoid } from "nanoid";
import { PartySocket } from "partysocket";
import { usePartySocket } from "partysocket/react";

function App() {
Expand Down Expand Up @@ -36,6 +37,21 @@ function App() {
data: `${socket.id} says hello!`
})
);

PartySocket.fetch(
{
host: window.location.host,
party: "pubsub", // the name of the party, use the binding's lowercase form
room: "default" // the name of the room/channel
},
{
method: "POST",
body: JSON.stringify({
topic: "topic-abc",
data: "hello from a post!"
})
}
).catch(console.error);
}, 2000);
}, [socket]);

Expand Down
22 changes: 17 additions & 5 deletions packages/partysub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,37 @@ import { PartySocket } from "partysocket";
const ws = new PartySocket({
host: "...", // the host of the partyserver, defaults to window.location.host
party: "pubsub", // the name of the party, use the binding's lowercase form
room: "default" // the name of the room/channel
room: "default", // the name of the room/channel
query: {
// by default, it subscribes to all topics
// but you can set it to specific topics
topics: [
"topic-abc", // a specific topic
"prefix:*" // a prefixed topic,
],
"topic-abc", // a specific topic
"prefix:*" // a prefixed topic,
]
}
});


// Listen to incoming messages
client.addEventListener("message", (event) => {
console.log(event.topic, event.data);
});

// publish a message to the server
ws.send(JSON.stringify({ topic: "topic-abc", data: "hello world" }));

// You can also POST a message to the server
PartySocket.fetch(
{
host: window.location.host, // the host of the partyserver
party: "pubsub", // the name of the party, use the binding's lowercase form
room: "default" // the name of the room/channel
},
{
method: "POST",
body: JSON.stringify({ topic: "topic-abc", data: "hello world" })
}
);
```

### react
Expand Down
17 changes: 17 additions & 0 deletions packages/partysub/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ export function createPubSubServer<Env = unknown>(options: {
return;
}

this.broadcastPubSubMessage(topic, data);
}

broadcastPubSubMessage(topic: string, data: string) {
this.publish(topic, data);
// also publish this message to all the other nodes
const namespace =
Expand Down Expand Up @@ -98,6 +102,19 @@ export function createPubSubServer<Env = unknown>(options: {
}
}
}

async onRequest(request: Request): Promise<Response> {
const { topic, data } = await request.json<{
topic: string;
data: string;
}>();
if (!topic || !data) {
return new Response("Invalid request", { status: 400 });
}
this.broadcastPubSubMessage(topic, data);

return new Response("OK");
}
}

async function routePubSubRequest(request: Request, env: Env) {
Expand Down

0 comments on commit 1985f63

Please sign in to comment.