Pub/Sub API
Real-time message fan-out with Server-Sent Events and WebSockets
Pub/Sub provides real-time message fan-out. Publishers send messages to a channel, and subscribers receive them over Server-Sent Events or WebSockets.
Endpoints
| Method | Endpoint | Description |
|---|---|---|
| POST | /{plateId}/pubsub/{channel}/publish | Publish a message |
| GET | /{plateId}/pubsub/{channel}/subscribe | Subscribe with SSE |
| GET | /{plateId}/pubsub/{channel}/ws | Subscribe with WebSocket |
| POST | /{plateId}/publish/{channel} | Legacy publish alias |
| GET | /{plateId}/subscribe/{channel} | Legacy SSE alias |
| GET | /{plateId}/ws/subscribe/{channel} | Legacy WebSocket alias |
Publish Simple Message
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]";
const response = await fetch(`${baseUrl}/${plateId}/pubsub/alerts/publish`, {
method: "POST",
headers: {
"Authorization": apiKey,
"Content-Type": "application/json"
},
body: JSON.stringify({
message: "Server restarted"
})
});
const data = await response.json();
console.log(data);Publish Structured Object
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]";
const response = await fetch(`${baseUrl}/${plateId}/pubsub/user:123:events/publish`, {
method: "POST",
headers: {
"Authorization": apiKey,
"Content-Type": "application/json"
},
body: JSON.stringify({
message: {
type: "login",
userId: "123",
timestamp: "2026-03-15T12:00:00Z"
}
})
});
const data = await response.json();
console.log(data);Subscribe with Server-Sent Events
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]";
const eventSource = new EventSource(`${baseUrl}/${plateId}/pubsub/alerts/subscribe`, {
headers: {
"Authorization": apiKey
}
});
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Received:", data);
};
eventSource.onerror = (error) => {
console.error("Connection closed", error);
eventSource.close();
};Subscribe with Pattern (SSE)
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]";
const eventSource = new EventSource(
`${baseUrl}/${plateId}/pubsub/user:*/subscribe?pattern=true`,
{
headers: {
"Authorization": apiKey
}
}
);
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Received:", data);
};Subscribe with WebSocket
const plateId = "[id]";
const apiKey = "your-api-key";
const baseUrl = "[base-url]".replace("http", "ws");
const ws = new WebSocket(`${baseUrl}/${plateId}/pubsub/alerts/ws`);
ws.onopen = () => {
ws.send(JSON.stringify({ auth: apiKey }));
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Received:", data);
};
ws.onerror = (error) => {
console.error("WebSocket error:", error);
};
ws.onclose = () => {
console.log("WebSocket closed");
};Notes
- Published payloads are JSON-encoded.
- Subscription payloads decode JSON when possible and fall back to plain strings.
- Legacy publish/subscribe routes are still supported.