# JavaScript SDK

URL: https://docs.folkways.io/docs/js-sdk

@folkwaysio/realtime connects web, mobile and Node.js apps to Folkways: publish and subscribe to live events with reconnects, token refresh and replay handled for you.

`@folkwaysio/realtime` is the JavaScript and TypeScript client for Folkways, [published on npm](https://www.npmjs.com/package/@folkwaysio/realtime). The shorter `@folkways/realtime` package name was not available on npm, so we publish under the `@folkwaysio` scope.

Your app connects once, then publishes and subscribes to **topics**. The SDK keeps the connection alive for you: it picks a transport, reconnects after network drops, refreshes tokens before they expire and resumes every subscription where it left off.

It runs in browsers, Node.js 20+, Deno, Bun and React Native. It ships ESM, CommonJS, TypeScript types and a browser bundle.

```ts
import { Folkways } from '@folkwaysio/realtime';

const client = new Folkways.Realtime({ url: 'https://rte.example.com', authUrl: '/api/realtime-token' });
await client.connect();

const room = client.topic('chat/lobby');
room.subscribe((msg) => console.log(msg.publisher, msg.data));
await room.publish('message', { text: 'hello' });
```

## What you need [#what-you-need]

1. A Folkways account with a **segment**, a **signing key** and **topic rules**. [Set up the dashboard](/docs/js-sdk/prerequisites).
2. A small endpoint on your backend that signs a token for the signed-in user. The SDK's server part does this in
   a few lines: see [Issue tokens from your backend](/docs/js-sdk/server-tokens).
3. The SDK in your app.

Apps that only display public data, such as a live scoreboard, can skip the backend and use a
[publishable key](/docs/js-sdk/publishable-keys) instead.

## Start here [#start-here]

<Cards>
  <Card title="Set up the dashboard" href="/docs/js-sdk/prerequisites" description="Create an account, a segment, a signing key and topic rules." />
  <Card title="Quick start" href="/docs/js-sdk/quick-start" description="Sign a token, connect, publish and subscribe in about 30 lines." />
  <Card title="Key concepts" href="/docs/js-sdk/concepts" description="Segments, topics, events, identities and tokens in plain words." />
  <Card title="Client options" href="/docs/js-sdk/reference/client-options" description="Every option of Folkways.Realtime, with its default." />
</Cards>
