SeuraaEdge
Docs/Getting Started
Getting Started

Connect a backend and browser client in a few minutes.

Quickstart

Connect an authenticated application user to a channel.

Create A Backend Client

ts
import { Client } from "@seuraa/edge-node";

const edge = new Client({
  appKey: process.env.SEURAA_APP_KEY!,
  appSecret: process.env.SEURAA_APP_SECRET!,
});

Create A Token Endpoint

Authenticate the request and authorize the requested channel before issuing a token.

ts
export async function POST(request: Request) {
  const user = await requireUser(request);
  const { channel } = await request.json();

  if (!canSubscribe(user, channel)) {
    return Response.json({ error: "Forbidden" }, { status: 403 });
  }

  const session = edge.auth.signIn({
    channel,
    expiresInSeconds: 60,
    permissions: ["subscribe"],
    userId: user.id,
  });

  return Response.json({ token: session.token });
}

The browser client sends this request body to the token endpoint:

ts
{
  appKey: "app-key",
  channel: `private:${userId}`,
}

Subscribe In The Browser

ts
import { Client } from "@seuraa/edge";

const edge = new Client({
  appKey: "app-key",
  auth: "/api/edge/token",
});

const userId = currentUser.id;
const channel = edge.channel(`private:${userId}`);

const off = channel.subscribe<{ title: string }>("notification.created", (event) => {
  console.log(event.data.title);
});

edge.channel(...) returns a local handle. It does not create a remote resource or connect until the handle is used.

Calling subscribe connects the channel. Call off() to remove this event handler, or channel.unsubscribe() to leave the channel.

Publish From Your Backend

ts
const userId = "user-123";

await edge.channels.publish(`private:${userId}`, "notification.created", {
  title: "Your report is ready",
});

The subscribed browser receives the event through its handler.