Skip to content

open-socket/opensocket-js

Repository files navigation

OpenSocket JavaScript SDK

πŸ”Œ Vendor-agnostic real-time communication for JavaScript/TypeScript

CI npm version License TypeScript

Overview

OpenSocket provides a unified API for real-time communication across multiple providers (Pusher, Ably, Socket.io, etc.), allowing you to switch providers without changing your application code.

πŸš€ Quick Start

npm install @open-socket/core
import { createOpenSocket } from '@open-socket/core';
import { PusherProvider } from '@open-socket/provider-pusher';

// Initialize with your chosen provider
const socket = await createOpenSocket({
  provider: new PusherProvider({
    key: 'your-pusher-key',
    cluster: 'us2',
  }),
});

// Connect to the provider
await socket.connect();

// Subscribe to a channel
const channel = socket.channel('my-channel');

// Listen for messages
await channel.subscribe('my-event', message => {
  console.log('Received:', message.data);
});

// Publish messages
await channel.publish('my-event', {
  text: 'Hello, real-time world!',
});

πŸ“¦ Packages

This monorepo contains the following packages:

Package Description Version
@open-socket/core Core abstractions and interfaces npm
@open-socket/testing-utils Testing utilities and mock provider npm

Provider Packages (Coming Soon)

  • @open-socket/provider-pusher - Pusher adapter
  • @open-socket/provider-ably - Ably adapter
  • @open-socket/provider-socketio - Socket.io adapter

Framework Packages (Coming Soon)

  • @open-socket/react - React hooks and components
  • @open-socket/vue - Vue composables
  • @open-socket/angular - Angular services

✨ Features

  • Vendor Agnostic: Switch between providers with a single config change
  • Type Safe: Full TypeScript support with strict typing
  • Lightweight: Core package <10KB gzipped
  • Extensible: Middleware system for customization
  • Well Tested: Comprehensive test suite for all providers
  • Modern: ES modules, tree-shaking, async/await

πŸ”„ Switching Providers

One of the key benefits of OpenSocket is the ability to switch providers easily:

// Using Pusher
const socket = await createOpenSocket({
  provider: new PusherProvider({ key: 'pusher-key' }),
});

// Switch to Ably (same API!)
const socket = await createOpenSocket({
  provider: new AblyProvider({ key: 'ably-key' }),
});

// Switch to Socket.io (same API!)
const socket = await createOpenSocket({
  provider: new SocketIOProvider({ url: 'http://localhost:3000' }),
});

πŸ§ͺ Testing

OpenSocket includes comprehensive testing utilities:

import { MockProvider, createProviderTestSuite } from '@open-socket/testing-utils';

// Use the mock provider for testing
const mockProvider = new MockProvider();
const socket = await createOpenSocket({ provider: mockProvider });

// Simulate events
mockProvider.simulateMessage('channel', 'event', { test: true });

// Run the standard test suite against your provider
createProviderTestSuite(() => new YourProvider(), 'YourProvider');

πŸ—οΈ Architecture

OpenSocket uses a layered architecture:

Application Code
     ↓
OpenSocket API (unified interface)
     ↓
Provider Interface (abstraction)
     ↓
Provider Adapter (implementation)
     ↓
Native Provider SDK

πŸ› οΈ Development

This project uses pnpm workspaces and Turborepo:

# Install dependencies
pnpm install

# Build all packages
pnpm build

# Run tests
pnpm test

# Run in development mode
pnpm dev

# Lint and format
pnpm lint
pnpm format

πŸ“Š Provider Feature Matrix

Feature Pusher Ably Socket.io
Public Channels βœ… βœ… βœ…
Private Channels βœ… βœ… βœ…
Presence βœ… βœ… βœ…
Message History ❌ βœ… ⚠️
Binary Data ❌ βœ… βœ…
Acknowledgments ❌ βœ… βœ…

πŸ”Œ Middleware

Extend OpenSocket with custom middleware:

const loggingMiddleware = {
  name: 'logging',
  pre: async context => {
    console.log('Before:', context.operation);
  },
  post: async context => {
    console.log('After:', context.operation);
  },
  error: async (error, context) => {
    console.error('Error:', error);
  },
};

socket.use(loggingMiddleware);

πŸ“š Documentation

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

πŸ“ License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

πŸ™ Acknowledgments

OpenSocket is inspired by:

  • OpenFeature for the vendor-agnostic approach
  • The real-time communication community

πŸ”— Links


Built with ❀️ by the OpenSocket community

About

No description, website, or topics provided.

Resources

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •