JavaScript client SDK for Apinator — real-time WebSocket messaging for web applications.
- Public, private, and presence channels
- Automatic reconnection with exponential backoff
- Client events on private/presence channels
- Presence member tracking
- TypeScript-first with full type definitions
- Zero dependencies — works in any modern browser
- Dual ESM + CommonJS builds
npm install @apinator/clientOr via CDN:
<script src="https://cdn.jsdelivr.net/npm/@apinator/client/dist/index.mjs" type="module"></script>import { Apinator } from '@apinator/client';
const client = new Apinator({
appKey: 'your-app-key',
cluster: 'eu',
});
client.connect();
const channel = client.subscribe('my-channel');
channel.bind('my-event', (data) => {
console.log('Received:', data);
});No authentication required. Any client can subscribe.
const channel = client.subscribe('news');
channel.bind('update', (data) => { /* ... */ });Require server-side authentication. Prefix with private-.
const client = new Apinator({
appKey: 'your-app-key',
cluster: 'eu',
authEndpoint: '/api/realtime/auth',
});
const channel = client.subscribe('private-orders');
channel.bind('new-order', (data) => { /* ... */ });Like private channels, but also track who is subscribed. Prefix with presence-.
const presence = client.subscribe('presence-chat') as PresenceChannel;
presence.bind('realtime:subscription_succeeded', () => {
console.log('Members:', presence.getMembers());
console.log('Me:', presence.me);
});
presence.bind('realtime:member_added', (member) => {
console.log('Joined:', member);
});
presence.bind('realtime:member_removed', (member) => {
console.log('Left:', member);
});channel.me is derived from the signed channel_data returned by your authEndpoint.
It becomes available only after realtime:subscription_succeeded.
Trigger events directly from the client on private or presence channels:
const privateChannel = client.subscribe('private-chat');
privateChannel.trigger('client-typing', { user: 'alice' });Monitor the connection lifecycle:
client.bind('state_change', ({ previous, current }) => {
console.log(`${previous} -> ${current}`);
});States: initialized → connecting → connected → disconnected / unavailable
See docs/api-reference.md for the full API.
Any browser with WebSocket and fetch support:
- Chrome 42+
- Firefox 39+
- Safari 10.1+
- Edge 14+
- Brave 10+
MIT — see LICENSE.