• Stars
    star
    59,936
  • Rank 145 (Top 0.01 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 14 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Realtime application framework (Node.JS server)

socket.io

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

Features

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

Some implementations in other languages are also available:

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:

Simple and convenient API

Sample code:

io.on('connection', socket => {
  socket.emit('request', /* … */); // emit an event to the socket
  io.emit('broadcast', /* … */); // emit an event to all connected sockets
  socket.on('reply', () => { /* … */ }); // listen to the event
});

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

// with npm
npm install socket.io

// with yarn
yarn add socket.io

How to use

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

const server = require('http').createServer();
const io = require('socket.io')(server);
io.on('connection', client => {
  client.on('event', data => { /* … */ });
  client.on('disconnect', () => { /* … */ });
});
server.listen(3000);

Standalone

const io = require('socket.io')();
io.on('connection', client => { ... });
io.listen(3000);

Module syntax

import { Server } from "socket.io";
const io = new Server(server);
io.listen(3000);

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, not the express application function. Also make sure to call .listen on the server, not the app.

const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);

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.

const app = require('koa')();
const server = require('http').createServer(app.callback());
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);

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.

const app = require('fastify')();
app.register(require('fastify-socket.io'));
app.io.on('connection', () => { /* … */ });
app.listen(3000);

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:

DEBUG=socket.io* node myapp

Testing

npm test

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

MIT

More Repositories

1

socket.io-client

Realtime application framework (client)
TypeScript
10,492
star
2

socket.io-client-java

Full-featured Socket.IO Client Library for Java, which is compatible with Socket.IO v1.0 and later.
Java
5,244
star
3

socket.io-client-swift

Swift
5,128
star
4

engine.io

The engine used in the Socket.IO JavaScript server, which manages the low-level transports such as HTTP long-polling and WebSocket.
JavaScript
4,569
star
5

socket.io-redis-adapter

Adapter to enable broadcasting of events to multiple separate socket.io server nodes.
TypeScript
2,712
star
6

socket.io-client-cpp

C++11 implementation of Socket.IO client
C++
2,181
star
7

socket.io-p2p

JavaScript
1,022
star
8

engine.io-client

The engine used in the Socket.IO JavaScript client, which manages the low-level transports such as HTTP long-polling, WebSocket and WebTransport.
JavaScript
738
star
9

socket.io-redis-emitter

The Socket.IO Redis emitter, allowing to communicate with a group of Socket.IO servers from another Node.js process.
TypeScript
712
star
10

socket.io-protocol

Socket.IO Protocol specification
JavaScript
494
star
11

engine.io-client-java

Engine.IO Client Library for Java
Java
356
star
12

socket.io-admin-ui

Admin UI for Socket.IO
Vue
322
star
13

socket.io-website

Socket.IO website and blog
JavaScript
311
star
14

engine.io-protocol

Engine.IO protocol
JavaScript
229
star
15

socket.io-adapter

The Socket.IO in-memory adapter
TypeScript
195
star
16

engine.io-server-java

Engine.IO Server Library for Java
Java
161
star
17

socket.io-parser

JavaScript
133
star
18

socket.io-deno

Socket.IO server for Deno
TypeScript
88
star
19

engine.io-parser

Parser for the engine.io protocol, used by client and server
TypeScript
75
star
20

socket.io-msgpack-parser

Socket.IO parser based on msgpack
JavaScript
62
star
21

socket.io-sticky

A simple and performant way to use Socket.IO within a cluster.
JavaScript
37
star
22

socket.io-mongo-adapter

The Socket.IO MongoDB adapter, allowing to broadcast events between several Socket.IO servers
TypeScript
21
star
23

socket.io-chat-platform

A basic chat platform based on Socket.IO
JavaScript
19
star
24

socket.io-postgres-adapter

The Socket.IO Postgres adapter, allowing to broadcast events between several Socket.IO servers
TypeScript
18
star
25

socket.io-redis-streams-adapter

The Socket.IO adapter based on Redis Streams, allowing to broadcast events between several Socket.IO servers.
TypeScript
17
star
26

socket.io-cluster-adapter

The Socket.IO official cluster adapter, allowing to broadcast events between several Socket.IO servers.
TypeScript
14
star
27

socket.io-json-parser

socket.io parser based on JSON.stringify / JSON.parse
JavaScript
12
star
28

socket.io-minimal-example

Socket.IO minimal example
HTML
8
star
29

socket.io-benchmarks

Benchmarks for Socket.IO
JavaScript
8
star
30

socket.io-compression-demo

JavaScript
7
star
31

get-started-drawing

7
star
32

lz77-compression-demo

JavaScript
7
star
33

socket.io-mongo-emitter

The Socket.IO MongoDB emitter, allowing to communicate with a group of Socket.IO servers from another Node.js process
TypeScript
6
star
34

socket.io-postgres-emitter

The Socket.IO Postgres emitter, allowing to communicate with a group of Socket.IO servers from another Node.js process.
TypeScript
5
star
35

socket.io-echo-server

Socket.IO echo server
JavaScript
4
star
36

socket.io-sample-playbook

This repository contains an Ansible playbook to set up a basic Socket.IO application.
JavaScript
4
star
37

tap-to-android

3
star
38

socket.io-swift-fiddle

Swift
2
star
39

socket.io-browsers

A reusable list of browsers to test when using defunctzombie/zuul
JavaScript
1
star