SOCKJS Logo
Back to Docs
Introduction

Getting Started

Learn how to install and set up SOCKJS in your project

1Installation

Install SOCKJS using npm or yarn:

bash
npm install sockjs-client
# or
yarn add sockjs-client

2CDN Installation

You can also include SOCKJS directly via CDN:

html
<script src="https://cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js"></script>

3Basic Usage

javascript
import SockJS from 'sockjs-client';

const sock = new SockJS('https://your-server.com/sockjs');

sock.onopen = function() {
  console.log('Connection opened');
  sock.send('Hello!');
};

sock.onmessage = function(e) {
  console.log('Message received:', e.data);
};

sock.onclose = function() {
  console.log('Connection closed');
};

4Browser Support

SOCKJS supports all modern browsers including:

  • Chrome 4+
  • Firefox 3+
  • Safari 5+
  • Opera 10.70+
  • IE 8+ (with fallbacks)
  • 5Configuration Options

    javascript
    const sock = new SockJS(url, null, {
      server: 'server-id',
      transports: ['websocket'],
      sessionId: 8,
      timeout: 5000
    });