Socket.IO

Socket.IO支持实时双向事件通信。(Socket.IO enables real-time bidirectional event-based communication. )

Github星跟蹤圖

Socket.IO 支持实时双向事件通信。它包括:

  • 一个Node.js服务器(这个存储库)
  • 浏览器的JavaScript客户端库(或Node.js客户端)

其他语言的一些实现也可用:

  • Java
  • C++
  • Swift

其主要特点是:

1. 可靠性

即使在以下情况下也能建立连接:

  • 代理和负载平衡器。
  • 个人防火墙和防病毒软件。

为此,它依赖于首先建立长轮询连接的Engine.IO,然后尝试升级到更好的传输,像 WebSocket 那样被“测试”。

2. 自动重新连接支持

除非另有指示,断开的客户端将尝试永久重新连接,直到服务器再次可用。请在这里查看可用的重新连接选项。

3. 断路检测

在 Engine.IO 级别实现心跳机制,允许服务器和客户端知道其他人何时不再响应。

该功能是通过在服务器和客户端上设置的定时器实现的,在连接握手期间共享超时值(pingInterval 和 pingTimeout 参数)。那些定时器需要任何后续的客户端调用被引导到同一个服务器,因此使用多个节点时的粘性会话需求。

4. 二进制支持

可以发出任何可序列化的数据结构,包括:

  • ArrayBuffer 和 Blob 在浏览器中
  • Node.juff 中的 ArrayBuffer 和 Buffer
5. 简单方便的 API

示例代码:

io.on('connection', function(socket){  socket.emit('request', /* */); //emit an event to the socket  io.emit('broadcast', /* */); //emit an event to all connected sockets  socket.on('reply', function(){ /* */}); //listen to the event});
6. 跨浏览器

浏览器支持在 Saucelabs 进行测试。

7. 多路复用支持

为了在您的应用程序中创建关注点(例如每个模块或基于权限),Socket.IO 允许您创建多个命名空间,这些命名空间将用作单独的通信通道,但将共享相同的底层连接。

8. 房间(Room)支持

在每个命名空间中,您可以定义插口可以加入和离开的任意频道,称为“房间”。然后,您可以播放到任何给定的房间,到达已加入的每个插槽。

这是一个有用的功能,可以将通知发送到一组用户,或者连接到多个设备上的给定用户。

注意:Socket.IO 不是 WebSocket 实现。虽然 Socket.IO 尽可能使用 WebSocket 作为传输,但它会在每个数据包中添加一些元数据:在需要消息确认时,数据包类型,命名空间和 ack id。这就是为什么 WebSocket 客户端将无法成功连接到 Socket.IO 服务器,Socket.IO 客户端也无法连接到 WebSocket 服务器(如ws://echo.websocket.org)。请参阅协议规范。

概覽

名稱與所有者socketio/socket.io
主編程語言TypeScript
編程語言JavaScript (語言數: 2)
平台Linux, Mac, Windows
許可證MIT License
發布數172
最新版本名稱4.7.5 (發布於 )
第一版名稱0.1 (發布於 2010-03-18 23:01:09)
創建於2010-03-11 18:24:48
推送於2024-04-12 09:14:24
最后一次提交2023-10-10 11:00:27
星數60.1k
關注者數1.5k
派生數10.1k
提交數2k
已啟用問題?
問題數3427
打開的問題數96
拉請求數425
打開的拉請求數9
關閉的拉請求數418
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

socket.io

Backers on Open Collective Sponsors on Open Collective
Build Status
Dependency Status
devDependency 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 Saucelabs:

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

npm install 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);

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.

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);

Documentation

Please see the documentation 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

去到頂部