Skip to content

wsocket-io/sdk-js

Repository files navigation

wSocket JavaScript SDK

Official JavaScript/TypeScript SDK for wSocket — Realtime Pub/Sub over WebSockets.

npm License: MIT

Installation

npm install @wsocket-io/sdk

CDN (Browser)

<script src="https://cdn.jsdelivr.net/npm/@wsocket-io/sdk@latest"></script>

Quick Start

import { createClient } from '@wsocket-io/sdk';

const client = createClient('wss://node00.wsocket.online', 'your-api-key');
client.connect();

const chat = client.channel('chat:general');

chat.subscribe((data, meta) => {
  console.log(`[${meta.channel}]`, data);
});

chat.publish({ text: 'Hello from JavaScript!' });

Features

  • Pub/Sub — Subscribe and publish to channels in real-time
  • Presence — Track who is online in a channel
  • History — Retrieve past messages
  • Push Notifications — Web Push via Service Workers
  • Connection Recovery — Automatic reconnection with message replay
  • TypeScript — Full type definitions included

Presence

const chat = client.channel('chat:general');

chat.presence.onEnter((member) => {
  console.log(`Joined: ${member.clientId}`);
});

chat.presence.onLeave((member) => {
  console.log(`Left: ${member.clientId}`);
});

chat.presence.enter({ name: 'Alice' });
const members = chat.presence.get();

History

chat.onHistory((result) => {
  result.messages.forEach((msg) => {
    console.log(`[${msg.timestamp}] ${JSON.stringify(msg.data)}`);
  });
});

chat.history({ limit: 50 });

Push Notifications

client.configurePush({
  vapidPublicKey: 'your-vapid-public-key',
  serviceWorkerPath: '/sw.js',
});

await client.push?.subscribe('alerts');

Requirements

  • Node.js >= 18 or modern browsers
  • ws >= 8.0 (Node.js only)

Development

npm install
npm run build
npm test

License

MIT

About

wSocket JavaScript/TypeScript SDK — Realtime Pub/Sub client

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors