socket.io

Realtime application framework (Node.JS server)

README

socket.io Run on Repl.it Backers on Open Collective Sponsors on Open Collective Build Status NPM version

Downloads undefined

Features


Socket.IO enables real-time bidirectional event-based communication. It consists of:

- a Node.js server (this repository)
- a Javascript client library for the browser (or a Node.js client)

Some implementations in other languages are also available:

- C++

Its main features are:

Reliability


Connections are established even in the presence of:
  - proxies and load balancers.
  - personal firewall and antivirus software.

For this purpose, it relies on Engine.IO, which first establishes a long-polling connection, then tries to upgrade to better transports that are "tested" on the side, like WebSocket. Please see the Goals section for more information.

Auto-reconnection support


Unless instructed otherwise a disconnected client will try to reconnect forever, until the server is available again. Please see the available reconnection options here.

Disconnection detection


A heartbeat mechanism is implemented at the Engine.IO level, allowing both the server and the client to know when the other one is not responding anymore.

That functionality is achieved with timers set on both the server and the client, with timeout values (the pingInterval and pingTimeout parameters) shared during the connection handshake. Those timers require any subsequent client calls to be directed to the same server, hence the sticky-session requirement when using multiples nodes.

Binary support


Any serializable data structures can be emitted, including:

- ArrayBuffer and Blob in the browser
- ArrayBuffer and Buffer in Node.js

Simple and convenient API


Sample code:

  1. ```js
  2. io.on('connection', socket => {
  3.   socket.emit('request', /* … */); // emit an event to the socket
  4.   io.emit('broadcast', /* … */); // emit an event to all connected sockets
  5.   socket.on('reply', () => { /* … */ }); // listen to the event
  6. });
  7. ```

Cross-browser


Browser support is tested in Sauce Labs:
Sauce Test Status

Multiplexing support


In order to create separation of concerns within your application (for example per module, or based on permissions), Socket.IO allows you to create several Namespaces, which will act as separate communication channels but will share the same underlying connection.

Room support


Within each Namespace, you can define arbitrary channels, called Rooms, that sockets can join and leave. You can then broadcast to any given room, reaching every socket that has joined it.

This is a useful feature to send notifications to a group of users, or to a given user connected on several devices for example.


Note: Socket.IO is not a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server (like ws://echo.websocket.org) either. Please see the protocol specification here.

Installation


  1. ```bash
  2. // with npm
  3. npm install socket.io

  4. // with yarn
  5. yarn add socket.io
  6. ```

How to use


The following example attaches socket.io to a plain Node.JS
HTTP server listening on port 3000.

  1. ```js
  2. const server = require('http').createServer();
  3. const io = require('socket.io')(server);
  4. io.on('connection', client => {
  5.   client.on('event', data => { /* … */ });
  6.   client.on('disconnect', () => { /* … */ });
  7. });
  8. server.listen(3000);
  9. ```

Standalone


  1. ```js
  2. const io = require('socket.io')();
  3. io.on('connection', client => { ... });
  4. io.listen(3000);
  5. ```

Module syntax


  1. ```js
  2. import { Server } from "socket.io";
  3. const io = new Server(server);
  4. io.listen(3000);
  5. ```

In conjunction with Express


Starting with 3.0, express applications have become request handler
functions that you pass to http or http Server instances. You need
to pass the Server to socket.io, and not the express application
function. Also make sure to call .listen on the server, not the app.

  1. ```js
  2. const app = require('express')();
  3. const server = require('http').createServer(app);
  4. const io = require('socket.io')(server);
  5. io.on('connection', () => { /* … */ });
  6. server.listen(3000);
  7. ```

In conjunction with Koa


Like Express.JS, Koa works by exposing an application as a request
handler function, but only by calling the callback method.

  1. ```js
  2. const app = require('koa')();
  3. const server = require('http').createServer(app.callback());
  4. const io = require('socket.io')(server);
  5. io.on('connection', () => { /* … */ });
  6. server.listen(3000);
  7. ```

In conjunction with Fastify


To integrate Socket.io in your Fastify application you just need to
register fastify-socket.io plugin. It will create a decorator
called io.

  1. ```js
  2. const app = require('fastify')();
  3. app.register(require('fastify-socket.io'));
  4. app.io.on('connection', () => { /* … */ });
  5. app.listen(3000);
  6. ```

Documentation


Please see the documentation here.

The source code of the website can be found here. Contributions are welcome!

Debug / logging


Socket.IO is powered by debug.
In order to see all the debug output, run your app with the environment variable
DEBUG including the desired scope.

To see the output from all of Socket.IO's debugging scopes you can use:

  1. ```
  2. DEBUG=socket.io* node myapp
  3. ```

Testing


  1. ```
  2. npm test
  3. ```
This runs the gulp task test. By default the test will be run with the source code in lib directory.

Set the environmental variable TEST_VERSION to compat to test the transpiled es5-compat version of the code.

The gulp task test will always transpile the source code into es5 and export to dist first before running the test.


Backers


Support us with a monthly donation and help us continue our activities. [Become a backer]



Sponsors


Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]



License