Back to Documentation

JavaScript SDK Examples

Real-world examples showing how to use the OddSockets JavaScript SDK in production applications

Try Interactive Demo
Basic Usage
Available
Simple example showing how to connect, subscribe to a channel, and publish messages using the core SDK functionality.
const OddSockets = require('@oddsocketsai/javascript-sdk');

const client = new OddSockets({
  apiKey: 'ak_live_1234567890abcdef'
});

const channel = client.channel('my-channel');

// Subscribe to messages
channel.subscribe((message) => {
  console.log('Received:', message);
});

// Publish a message
channel.publish('Hello, World!');
PubNub Migration
Available
Drop-in replacement example showing how to migrate from PubNub to OddSockets with minimal code changes.
// Replace PubNub with OddSockets
const { PubNubCompat } = require('@oddsocketsai/javascript-sdk');

const pubnub = new PubNubCompat({
  publishKey: 'ak_live_1234567890abcdef',
  subscribeKey: 'ak_live_1234567890abcdef',
  userId: 'user123'
});

// Same API as PubNub!
pubnub.addListener({
  message: function(messageEvent) {
    console.log('Message:', messageEvent.message);
  }
});

pubnub.subscribe({
  channels: ['my-channel']
});