Socket
Socket
Sign inDemoInstall

sockjs

Package Overview
Dependencies
53
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    sockjs


Version published
Maintainers
1
Created

Package description

What is sockjs?

SockJS is a JavaScript library that provides a WebSocket-like object in environments where true WebSockets are not available or not fully supported. It is designed to work both on the client-side and server-side to enable seamless communication between the two. SockJS provides a consistent API and attempts to provide a low-latency, full duplex, cross-domain communication channel that can work even behind restrictive corporate proxies.

What are sockjs's main functionalities?

Server-side WebSocket emulation

This code sample demonstrates how to create a SockJS server that emulates a WebSocket. It listens for connections on the '/echo' path and simply echoes back any messages it receives.

const http = require('http');
const sockjs = require('sockjs');
const echo = sockjs.createServer({ prefix: '/echo' });
echo.on('connection', function(conn) {
  conn.on('data', function(message) {
    conn.write(message);
  });
});
const server = http.createServer();
echo.installHandlers(server, { prefix: '/echo' });
server.listen(9999, '0.0.0.0');

Client-side WebSocket emulation

This code sample shows how to create a SockJS client that connects to a SockJS server. It sends a message when the connection is opened and logs any messages it receives before closing the connection.

const SockJS = require('sockjs-client');
let sock = new SockJS('http://localhost:9999/echo');
sock.onopen = function() {
  console.log('open');
  sock.send('test');
};
sock.onmessage = function(e) {
  console.log('message', e.data);
  sock.close();
};
sock.onclose = function() {
  console.log('close');
};

Other packages similar to sockjs

Readme

Source

SockJS-node server

To install sockjs-node run:

npm install sockjs

A fully working echo server would look like:

var http = require('http');
var sockjs = require('sockjs');

var sockjs_opts = {sockjs_url: "http://127.0.0.1:8000/lib/sockjs.js"};

var sjs_echo = new sockjs.Server(sockjs_opts);
sjs_echo.on('open', function(conn) {
    conn.on('message', function(e) {
        conn.send(e.data);
    });
});

var normal_handler = function(req, res) {
        res.writeHead(404);
        res.end("Not found.");
};

var server = http.createServer();
server.addListener('request', normal_handler);
server.addListener('upgrade', normal_handler);

sjs_echo.installHandlers(server, {prefix:'[/]echo'});

server.listen(9999, '0.0.0.0');

FAQs

Last updated on 11 Aug 2011

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc