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
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.
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 the following JSON body to this endpoint:
{
"appKey": "app-key",
"channel": "private:user-123"
}Subscribe In The Browser
import { Client } from "@seuraa/edge";
const edge = new Client({
appKey: "app-key",
auth: "/api/edge/token",
});
const channel = edge.channel("private:user-123");
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
await edge.channels.publish("private:user-123", "notification.created", {
title: "Your report is ready",
});The subscribed browser receives the event through its handler.