Quickstart

Get a working pub/sub loop with @qpub/sdk in a few minutes. Use the API and Language controls (right rail on large screens; on smaller screens, open the docs navigation drawer at the bottom of the page) to switch Socket vs REST and JavaScript vs React — samples and notes update together.

Prerequisites

  • A QPub account and project
  • An API key from the dashboard
  • Node.js, Bun, or a browser environment

1. Install

npm install @qpub/sdk

2. Receive or publish

Subscribe over the WebSocket client. By default the Socket connects automatically (autoConnect: true) to wss://socket.qpub.io/v1.

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("demo");
  await channel.subscribe((message) => {
    console.log("received", message.data, message.event);
  });
});

Do not put a production API key secret in a public browser app. Use token auth instead.

3. Complete the loop

In another process (or tab), publish with REST:

import { QPub } from "@qpub/sdk";

const rest = new QPub.Rest({
  apiKey: process.env.QPUB_API_KEY,
});

await rest.channels.get("demo").publish(
  { text: "Hello, QPub!" },
  { event: "greeting" }
);

Or publish from the same Socket after you are connected:

await socket.channels.get("demo").publish(
  { text: "Hello from socket" },
  { event: "greeting" }
);

4. Optional: event filter

await channel.subscribe(
  (message) => console.log(message.data),
  { event: "greeting" }
);

Local development

Override hosts when pointing at a local QPub stack:

const socket = new QPub.Socket({
  apiKey: process.env.QPUB_API_KEY,
  wsHost: "localhost",
  wsPort: 8131,
  isSecure: false,
});

const rest = new QPub.Rest({
  apiKey: process.env.QPUB_API_KEY,
  httpHost: "localhost",
  httpPort: 8111,
  isSecure: false,
});

Next steps