Getting Started

Examples

Working example apps you can run and learn from

Next.js Chat App

A full Next.js application with three interactive demos, each showcasing a different SDK feature.

cd examples/chat-app
bun install
bun run dev  # runs on port 3002

Demo pages

RouteWhat it shows
/chatBasic real-time chat — subscribe, publish, message list
/presenceWho's online — onPresence, offPresence, join/leave events
/typedType-safe messaging — ErebusPubSubSchemas with Zod validation

How it works

Server (src/app/api/erebus/[...all]/route.ts):

The API route uses createRouteHandler from @erebus-sh/sdk/server/next to handle grant token generation. It creates an ErebusService with your API key, prepares a session, joins the requested channel, and allows read-write access to all topics.

Client (each page):

Every page creates an ErebusPubSubClient using the factory:

import { ErebusClient, ErebusClientState } from "@erebus-sh/sdk/client";

const client = ErebusClient.createClient({
  client: ErebusClientState.PubSub,
  authBaseUrl: "",  // same-origin — hits /api/erebus/pubsub/grant
  wsBaseUrl: process.env.NEXT_PUBLIC_WS_URL,
});

client.joinChannel("demo");
await client.connect();

Then subscribes and publishes:

await client.subscribe("chat", (msg) => {
  const data = JSON.parse(msg.payload as string);
  // handle message
});

await client.publish("chat", JSON.stringify({ text: "hello", user: "alice" }));

Plain TypeScript

A minimal example that runs with bun run index.ts — no framework, no UI. Good for understanding the raw SDK API.

cd examples/chat-ts
bun install
bun run index.ts

Demonstrates: client creation, auth server with createGenericAdapter, channel joining, subscribe, publish, presence tracking.


Typed TypeScript

Same as the plain example but wraps the client with ErebusPubSubSchemas for compile-time and runtime type safety using Zod schemas.

cd examples/chat-ts-typed
bun install
bun run index.ts

Terminal UI (TUI)

An interactive terminal chat app built with Ink (React for CLIs). Features login, chat creation, SQLite message persistence, and real-time sync.

cd examples/chat-tui
bun install
bun run index.tsx

Login with user / user.