Documentation

Quickstart

Install the JavaScript SDK from npm. Send your first message between two browser tabs.

Video: Build a realtime chat. The YouTube player loads when you click play.

This is the shortest path to a working realtime chat. Clone the demo, add your keys, and start a small backend and a chat page. Open the page in two tabs. Send a message from one tab. The message arrives in the other tab.

Your example application runs on your computer. The SDK connects to Celeris. You do not need to build the SDK or run a Celeris server locally.

A channel is a connection to a named communication space. A segment is a named stream inside it. This demo uses channel demo-room and segment chat.

A channel connection automatically joins the default segment of the channel. You can access it with channel.defaultSegment(), without a separate subscribe() call. This demo uses an additional segment, chat, to show explicit subscriptions. The connection still joins default, but the demo's credentials grant messaging access only to chat.

Before you start

You need:

  • Node.js 24.15 or newer in the 24.x line, with npm.
  • Git, a current browser, and two terminal windows.
  • A Celeris application dedicated to development.

In Manage Apps, select your development application. Open API Keys. Get the application's client ID and signing secret. The signing secret is different from the client secret.

About this example: Authentication is only a comment here, and a small object stands in for your user database. Keep the example on your computer, and use only development credentials. Before other people use it, add real sign-in and authorization, as Authentication shows.

Step 1: get the code

git clone https://github.com/useceleris/demo.git
cd demo/quickstart_js

The demo has two parts:

quickstart_js/
  backend/
    .env.example
    server.ts         signs credentials
  frontend/
    src/
      App.vue         the sign-in screen
      ChatRoom.vue    connects to Celeris and shows the room

Both parts use TypeScript. The backend uses the server package, @useceleris/server, and Express. Node runs server.ts directly, so the backend has no build step. The frontend uses the client package, @useceleris/client, with Vue and Vite. The SDK does not need a framework.

Step 2: start the backend

cd backend
npm install
cp .env.example .env

Open .env and replace the two placeholder values:

backend/.env
CELERIS_CLIENT_ID=your-client-id
CELERIS_SIGNING_SECRET=your-signing-secret

The keys stay on the backend. Never put them in browser code. Do not commit .env.

Then start the backend:

npm start

It shows Backend: http://127.0.0.1:4000.

Step 3: start the frontend

In a second terminal, from demo/quickstart_js:

cd frontend
npm install
npm run dev

Vite serves the page at http://localhost:5173. It sends /api requests to the backend.

Step 4: send a message

  1. Open http://localhost:5173 in two tabs.
  2. Join as Ada in the first tab and as Grace in the second tab.
  3. Wait until both tabs show connected in the header.
  4. Send a message from the first tab. The second tab shows it from Ada.
  5. Reply from the second tab.

The page also works on a narrow screen, such as a phone.

How it works

The backend signs access

backend/server.ts holds the signing secret. The page asks it for credentials for one user and one room:

backend/server.ts
app.post("/api/realtime-credentials", (req, res) => {
  // Add your own auth here: find the signed-in user from the session.
  // This demo trusts the user ID that the page sends.
  const userId: string = req.body.userId;
  const room: string = req.body.channelReference;

  if (!users[userId]?.rooms.includes(room)) {
    res.status(403).json({ error: "Not allowed." });
    return;
  }

  res.json(
    signer.sign({
      channels: { kind: "restricted", references: [room] },
      permissions: {
        kind: "restricted",
        segments: [{ segmentId: "chat", read: true, write: true }],
      },
      reference: userId,
    }),
  );
});

users is a small object that stands in for your database. createSigner() makes signer from the two values in .env. The reference is the identity that other connections see.

The page connects

frontend/src/ChatRoom.vue holds all of the Celeris code in the page. The page shows this component when you join. Its <script setup> creates the client and the channel:

frontend/src/ChatRoom.vue
// The browser asks your backend for credentials. It never sees the secret.
const client = createClient({
  credentialProvider: async (request) => {
    const response = await fetch("/api/realtime-credentials", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({ userId: props.me.id, channelReference: request.channelReference }),
    });
    if (!response.ok) throw new Error("Credentials: " + response.status);
    return response.json();
  },
});

const channel = client.channel("demo-room");
const chat = channel.segment("chat");

channel.events().onStateChange((next) => (state.value = next));
chat.onMessage((payload, meta) => {
  show({ from: meta.tokenReference, text: readText(payload), at: Number(meta.timestamp) });
});
chat.subscribe();

onMounted(() => channel.connect().catch(console.error));
onBeforeUnmount(() => channel.close());

The SDK calls the credential provider before each connection attempt. subscribe() asks for the messages in chat. The component connects when it appears and closes the channel when it goes away.

Your own messages

send() publishes the text, then shows a local copy:

frontend/src/ChatRoom.vue
async function send() {
  const text = draft.value.trim();
  if (!text) return;

  draft.value = "";
  await chat.publish({ payload: textPayload(text) });
  // A connection does not get its own messages by default: show a local copy.
  show({ from: props.me.id, text, at: Date.now() });
}

Solve common problems

SymptomCheck
The header shows failedMake sure that both processes run. Check the client ID and the signing secret in backend/.env. Then reload the page.
The credential request returns 403The user must be in the users object, and the room must be demo-room.
No messages arrive in the other tabWait until both tabs show connected before you send.
Vite says the port is in useStop the other process that uses port 5173.

Next steps

  1. Authentication: replace the comment and the users object with real sign-in.
  2. JavaScript SDK: continue through messaging, payloads, presence, and server-side usage.
  3. Reconnection and Recovery: learn what happens when a connection drops.

We use Google cookies for analytics and for advertising measurement, only if you allow them. Until you choose, Google sets no cookies. Refer to our Cookie Policy.