Publish
Publish to a named channel with publish permission. Use the API and Language controls to switch Socket vs REST and JavaScript vs React.
Publish a message
Publish over the WebSocket client after you are connected.
import { QPub } from "@qpub/sdk";
const socket = new QPub.Socket({
apiKey: process.env.QPUB_API_KEY,
});
socket.connection.on("connected", async () => {
const channel = socket.channels.get("notifications");
await channel.publish(
{ title: "New order", orderId: "ord_1" },
{ event: "order.created", alias: "api" }
);
});Socket publish does not return the REST published payload.
Prefer a JWT or token request in SocketProvider for browsers instead of a production API key secret.
Publish over HTTP with the REST client (https://rest.qpub.io/v1).
import { QPub } from "@qpub/sdk";
const rest = new QPub.Rest({
apiKey: process.env.QPUB_API_KEY,
});
await rest.channels.get("notifications").publish(
{ title: "New order" },
{ event: "order.created" }
);HTTP:
POST /v1/channel/:channelName/messages
{
"messages": [
{
"event": "order.created",
"data": { "title": "New order" }
}
]
}
Successful publishes return 201 with a published array (channel, message_id, published_at, payload_count). Partial multi-channel failures may return 207 (PARTIAL_CONTENT) with those fields under details.
Run REST publish from a trusted server or script that can hold the API key secret.
Call REST from an event handler or server action. Keep secrets off the public client bundle.
Batch publish (REST)
Publish the same messages to multiple channels:
await rest.channels.publishBatch(
["notifications", "ops"],
[
{ data: { title: "New order" }, event: "order.created" },
]
);HTTP:
POST /v1/channels/messages
{
"channels": ["notifications", "ops"],
"messages": [
{ "event": "order.created", "data": { "title": "New order" } }
]
}
Batch publish to multiple channels is a REST feature (publishBatch / POST /v1/channels/messages). Switch to REST above, or publish once per channel on the Socket.
curl
API_KEY="publicId:secret"
curl -X POST "https://rest.qpub.io/v1/channel/notifications/messages" \
-H "Authorization: Basic $(echo -n "$API_KEY" | base64)" \
-H "Content-Type: application/json" \
-d '{"messages":[{"event":"ping","data":{"ok":true}}]}'
Notes
- At least one message is required in the REST body.
- Message size and rate limits depend on your plan.
- Subscribers need a Socket subscription to receive messages in realtime.