Skip to content

You need an app key and a page served from an origin the app allows.

The demo app works on http://localhost and http://127.0.0.1 (any port), allows up to 16 peers a room, and is shared by everyone trying jsflow. Its key is pk_O6puVNC7RYNarF7gt1YYQ3dDccBiX90denbw. For your own app, with your own origins and limits, create one in the dashboard; it gives you a key and this same snippet with the key filled in.

Save this as index.html:

index.html
<script type="module">
const { join, text } = await import(
'https://cdn.jsflow.com/v1.1.3/jsflow.js'
);
let room;
try {
// The demo key. The demo app is shared, so pick a room name of
// your own.
room = await join({
app: 'pk_O6puVNC7RYNarF7gt1YYQ3dDccBiX90denbw',
room: 'try-k3Hq9xZ2',
});
} catch (e) {
// A JoinError: its code says why, for example 'origin' (see
// Troubleshooting).
console.error('could not join', e);
throw e;
}
console.log('my id', room.id);
// 'joining' → 'connected'
room.on('state', (s) => console.log('state', s));
room.on('message', (from, data, info) => {
console.log(from, text(data), info);
});
room.on('peer', (id, up) => {
console.log('peer', id, up ? 'here' : 'gone');
if (up) room.send(id, 'hi, ' + id); // to one peer
});
// To everyone in the room, once a second.
setInterval(() => room.broadcast('tick ' + Date.now()), 1000);
</script>

Serve the page over HTTP from localhost, in its folder:

Terminal window
python3 -m http.server 8080

No Python? npx serve -l 8080 does the same. Open http://localhost:8080/ in two windows side by side, or in two browsers, with the developer console open. In the first window the console shows state joining and then, within a couple of seconds of the second window opening, peer <id> here and the other window’s hi and tick messages; the second window shows the same. A page opened from file:// has no origin the service accepts, so it must come from a server. If the console shows could not join with a JoinError whose code is origin, the address in the browser is not one the app allows: http://localhost:8080 and http://127.0.0.1:8080 count as different origins, and the demo app allows both. Every other code is in Troubleshooting. The browser must be Chrome or Edge 137, Safari 17 or Firefox 130 or newer: an older one cannot join (it gets unsupported_browser).

In Chrome, two tabs work too. Safari and phones take a background tab out of the room until it is shown again (see lifecycle).

Terminal window
npm install jsflow-sdk
app.ts
import { join, text } from 'jsflow-sdk';
const room = await join({ app: 'pk_…', room: 'lobby' });
room.on('message', (from, data) => console.log(from, text(data)));
room.broadcast('hello');

Vite and webpack bundle the SDK and its worker for you. Loading the SDK covers the CDN, other bundlers, a pinned version with an integrity hash, and the Content-Security-Policy.

Attach your listeners right after await join(), before awaiting anything else. No event is lost then: events reach the page as separate tasks, after your code that follows the await has run. If you await something else first, early peer and message events can pass with no listener.