Variables

These are yor variables typically stored in environment variables.

.env

SERVER_REGION=us3
SERVER_PASSWORD=abc123
SERVER_ID=123987
Client Stream

On your client/page, add this code in order to receive events from the server.

The createClientStream function takes a region value used to identify which server to hit. It returns multiple functions one of which is the authenticate function which takes a token as an argument and is used to authenticate the client.

import { createClientStream } from "streamthing";

const stream = await createClientStream(process.env.SERVER_REGION);
const res = await fetch("/api/get-streamthing-token?id=" + stream.id);
const data = await res.json();
stream.authenticate(data.token);
Server Stream

On your server/API endpoint, add this code in order to send events to the client.

The createServerStream function takes an object of arguments. The id and region values are used to identify the server. The channel is used to identify a certain group of messages all related to something e.g "chat-123". The password value is used to authenticate requests to make a server stream and send messages to the server.

import { createServerStream } from "streamthing";

const stream = createServerStream({
  id: process.env.SERVER_ID,
  region: process.env.SERVER_REGION,
  password: process.env.SERVER_PASSWORD,
  channel: "main",
});

createClientStream()
createServerStream()