@reactor-cloud/realtime
v0.3.0
Published
Realtime client for the Reactor JS SDK (WebSocket channels: postgres changes, broadcast, presence)
Maintainers
Readme
@reactor-cloud/realtime
Realtime client for Reactor. Subscribe to database changes, broadcast events, and track presence over a single multiplexed WebSocket.
Installation
npm install @reactor-cloud/realtime @reactor-cloud/sharedOr use the unified client:
npm install @reactor-cloud/clientQuick Start
import { RealtimeClient } from '@reactor-cloud/realtime';
const realtime = new RealtimeClient(ctx);
// Subscribe to database changes
const channel = realtime.channel('db-changes');
channel
.on('postgres_changes', {
event: 'INSERT',
schema: 'public',
table: 'messages',
}, (payload) => {
console.log('New message:', payload.new);
})
.subscribe();
// Broadcast messages
const broadcastChannel = realtime.channel('room:123');
broadcastChannel
.on('broadcast', { event: 'typing' }, (payload) => {
console.log('User typing:', payload);
})
.subscribe();
// Send broadcast
broadcastChannel.send({
type: 'broadcast',
event: 'typing',
payload: { userId: '123' },
});
// Presence tracking
const presenceChannel = realtime.channel('online-users');
presenceChannel
.on('presence', { event: 'sync' }, () => {
const state = presenceChannel.presenceState();
console.log('Online users:', state);
})
.subscribe(async (status) => {
if (status === 'SUBSCRIBED') {
await presenceChannel.track({ user_id: '123', online_at: new Date() });
}
});
// Unsubscribe
channel.unsubscribe();
realtime.removeChannel(channel);Node.js
Browsers provide WebSocket globally. In Node without a global WebSocket, pass an implementation (e.g. the ws package):
import WebSocket from 'ws';
const reactor = createClient(url, { key, realtime: { WebSocket } });Documentation
License
MIT
