Skip to main content

Quickstart: NodeJS

Build a real-time pub/sub app in 5 minutes.

Prerequisites

Install

npm install relayx-js

Publisher

Create publisher.js:

import { Realtime } from 'relayx-js';

const client = new Realtime({
api_key: 'your-api-key',
secret: 'your-secret'
});

await client.init();
await client.connect();

await client.publish('chat.room1', {
user: 'alice',
text: 'Hello RelayX!'
});

console.log('Message sent!');
await client.close();

Subscriber

Create subscriber.js:

import { Realtime } from 'relayx-js';

const client = new Realtime({
api_key: 'your-api-key',
secret: 'your-secret'
});

await client.init();
await client.connect();

await client.on('chat.room1', (msg) => {
console.log(`${msg.data.user}: ${msg.data.text}`);
});

console.log('Listening for messages...');

Run It

# Terminal 1 - start subscriber
node subscriber.js

# Terminal 2 - send a message
node publisher.js

You should see alice: Hello RelayX! appear in the subscriber terminal.