Socket.IO 是一个为实时应用提供跨平台实时通信的库。目标是使实时应用在每个浏览器和移动设备上成为可能,模糊不同的传输机制之间的差异。它使用了浏览器支持并采用的 HTML5 WebSocket 标准。以下内容为翻译官网 https://socket.io/docs/ 。
聊天应用程序 demo 地址 https://socket.io/demos/chat/
What Socket.IO is
Socket.IO is a library that enables real-time, bidirectional and event-based communication between the browser and the server. It consists of:
- a Node.js server: Source | API
- a Javascript client library for the browser (which can be also run from Node.js): Source | API
Socket.IO 是一个库,可以在浏览器和服务器之间实现实时,双向和基于事件的通信。它包括:
- Node.js 服务器
- 浏览器的 Javascript 客户端库(也可以从 Node.js 运行)
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.
即使存在以下情况,也建立了连接:
- 代理和负载均衡器。
- 个人防火墙和防病毒软件。
它依赖于 Engine.IO,它首先建立一个长轮询连接,然后尝试升级到更好的传输,这些传输在侧面被“测试”,比如 WebSocket。
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.
在 Engine.IO 级别实现心跳机制,允许服务器和客户端知道另一个机制何时不响应。
通过在服务器和客户端上设置定时器来实现该功能,在连接握手期间共享超时值(pingInterval 和 pingTimeout 参数)。这些计时器需要将任何后续客户端调用定向到同一服务器,因此在使用多个节点时会出现粘性会话要求。
Binary support 二进制支持
Any serializable data structures can be emitted, including:
- ArrayBuffer and Blob in the browser
- ArrayBuffer and Buffer in Node.js
可以发出任何可序列化的数据结构,包括:
- 浏览器中的 ArrayBuffer 和 Blob
- Node.js 中的 ArrayBuffer 和 Buffer
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.
为了在应用程序中创建关注点分离(例如,每个模块或基于权限),Socket.IO 允许您创建多个命名空间,这些命名空间将充当单独的通信通道,但将共享相同的底层连接。
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.
Those features come with a simple and convenient API, which looks like the following:
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 });
在每个命名空间中,您可以定义套接字可以加入和离开的任意通道,称为 Rooms。然后,您可以广播到任何给定的房间,到达已加入它的每个套接字。
这是一个非常有用的功能,可以将通知发送给一组用户,或者发送给连接在多个设备上的给定用户。
这些功能带有一个简单方便的 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 });
Using with Express
Server (app.js)
1. var app = require('express')(); 2. var server = require('http').Server(app); 3. var io = require('socket.io')(server); 4. 5. server.listen(80); 6. // WARNING: app.listen(80) will NOT work here! 7. 8. app.get('/', function (req, res) { 9. res.sendFile(__dirname + '/index.html'); 10. }); 11. 12. io.on('connection', function (socket) { 13. socket.emit('news', { hello: 'world' }); 14. socket.on('my other event', function (data) { 15. console.log(data); 16. }); 17. }); Client (index.html) 1. <script src="/socket.io/socket.io.js"></script> 2. <script> 3. var socket = io.connect('http://localhost'); 4. socket.on('news', function (data) { 5. console.log(data); 6. socket.emit('my other event', { my: 'data' }); 7. }); 8. </script>
Sending and receiving events
Socket.IO allows you to emit and receive custom events. Besides connect, message and disconnect, you can emit custom events:
Socket.IO 允许您发出和接收自定义事件。除了connect,message 和disconnect 之外,您还可以发出自定义事件:
// note, io(<port>) will create a http server for you var io = require('socket.io')(80); io.on('connection', function (socket) { io.emit('this', { will: 'be received by everyone'}); socket.on('private message', function (from, msg) { console.log('I received a private message by ', from, ' saying ', msg); }); socket.on('disconnect', function () { io.emit('user disconnected'); }); });
Restricting yourself to a namespace 限制自己使用命名空间
If you have control over all the messages and events emitted for a particular application, using the default / namespace works. If you want to leverage 3rd-party code, or produce code to share with others, socket.io provides a way of namespacing a socket.
This has the benefit of multiplexing a single connection. Instead of socket.io using two WebSocket connections, it’ll use one.
如果您可以控制为特定应用程序发出的所有消息和事件,则使用默认/命名空间。如果您想利用第三方代码,或生成与其他人共享的代码,socket.io提供了一种命名空间套接字的方法。
这具有多路复用单个连接的优点。而不是使用两个 WebSocket 连接的socket.io,它将使用一个。
// Server (app.js) var io = require('socket.io')(80); var chat = io .of('/chat') .on('connection', function (socket) { socket.emit('a message', { that: 'only' , '/chat': 'will get' }); chat.emit('a message', { everyone: 'in' , '/chat': 'will get' }); }); var news = io .of('/news') .on('connection', function (socket) { socket.emit('item', { news: 'item' }); }); // Client (index.html) <script> var chat = io.connect('http://localhost/chat') , news = io.connect('http://localhost/news'); chat.on('connect', function () { chat.emit('hi!'); }); news.on('news', function () { news.emit('woot'); }); </script>
Sending volatile messages 发送易失性消息(类似消息过期)
Sometimes certain messages can be dropped. Let’s say you have an app that shows realtime tweets for the keyword bieber.
If a certain client is not ready to receive messages (because of network slowness or other issues, or because they’re connected through long polling and is in the middle of a request-response cycle), if it doesn’t receive ALL the tweets related to bieber your application won’t suffer.
In that case, you might want to send those messages as volatile messages.
有时可以删除某些消息。假设您有一个应用程序,显示关键字bieber的实时推文。
如果某个客户端尚未准备好接收消息(由于网络速度缓慢或其他问题,或者因为它们通过长轮询连接并处于请求 - 响应周期的中间),如果它没有收到所有推文与 bieber 相关的申请不会受到影响。
在这种情况下,您可能希望将这些消息作为易失性消息发送。
var io = require('socket.io')(80); io.on('connection', function (socket) { var tweets = setInterval(function () { getBieberTweet(function (tweet) { socket.volatile.emit('bieber tweet', tweet); }); }, 100); socket.on('disconnect', function () { clearInterval(tweets); }); });
Sending and getting data (acknowledgements) 发送和获取数据(确认)
Sometimes, you might want to get a callback when the client confirmed the message reception.
To do this, simply pass a function as the last parameter of .send or .emit. What’s more, when you use .emit, the acknowledgement is done by you, which means you can also pass data along:
有时,您可能希望在客户端确认消息接收时收到回调。
为此,只需将函数作为.send或.emit的最后一个参数传递。更重要的是,当你使用.emit时,确认是由你完成的,这意味着你也可以传递数据:
// Server (app.js) var io = require('socket.io')(80); io.on('connection', function (socket) { socket.on('ferret', function (name, word, fn) { fn(name + ' says ' + word); }); });// Client (index.html) <script> var socket = io(); // TIP: io() with no args does auto-discovery socket.on('connect', function () { // TIP: you can avoid listening on `connect` and listen on events directly too! socket.emit('ferret', 'tobi', 'woot', function (data) { // args are sent in order to acknowledgement function console.log(data); // data will be 'tobi says woot' }); }); </script>
- 广播消息
To broadcast, simply add a broadcast flag to emit and send method calls. Broadcasting means sending a message to everyone else except for the socket that starts it.
要进行广播,只需添加广播标志即可发出和发送方法调用。广播意味着向除了启动它的套接字之外的所有人发送消息。
Rooms and Namespaces
Namespaces 命名空间
Socket.IO allows you to “namespace” your sockets, which essentially means assigning different endpoints or paths.
This is a useful feature to minimize the number of resources (TCP connections) and at the same time separate concerns within your application by introducing separation between communication channels.
Socket.IO 允许您“命名空间”套接字,这实际上意味着分配不同的端点或路径。
这是一个有用的功能,可以最大限度地减少资源(TCP连接)的数量,同时通过在通信通道之间引入分离来分离应用程序中的问题。
Default namespace 默认命名空间
We call the default namespace / and it’s the one Socket.IO clients connect to by default, and the one the server listens to by default.
我们调用默认名称空间/它是默认情况下连接到的一个 Socket.IO 客户端,以及默认情况下服务器监听的那个。
Custom namespaces
To set up a custom namespace, you can call the of function on the server-side:
1. const nsp = io.of('/my-namespace'); 2. nsp.on('connection', function(socket){ 3. console.log('someone connected'); 4. }); 5. nsp.emit('hi', 'everyone!'); On the client side, you tell Socket.IO client to connect to that namespace: 1. const socket = io('/my-namespace');
Rooms
Within each namespace, you can also define arbitrary channels that sockets can join and leave.
在每个命名空间内,您还可以定义套接字可以加入和离开的任意通道。
Default room 默认房间
Each Socket in Socket.IO is identified by a random, unguessable, unique identifier Socket#id. For your convenience, each socket automatically joins a room identified by this id.
This makes it easy to broadcast messages to other sockets:
Socket.IO 中的每个 Socket 由随机的,不可思议的唯一标识符 Socket#id 标识。为方便起见,每个套接字自动加入由此 ID 标识的房间。
这样可以轻松地将消息广播到其他套接字:
io.on('connection', function(socket){ socket.on('say to someone', function(id, msg){ socket.broadcast.to(id).emit('my message', msg); }); });
Sending messages from the outside-world 跨命名空间或房间发送消息
In some cases, you might want to emit events to sockets in Socket.IO namespaces / rooms from outside the context of your Socket.IO processes.
There’s several ways to tackle this problem, like implementing your own channel to send messages into the process.
To facilitate this use case, we created two modules:
- socket.io-redis
- socket.io-emitter
在某些情况下,您可能希望从 Socket.IO 进程的上下文之外向Socket.IO 命名空间 / 房间中的套接字发出事件。
有几种方法可以解决这个问题,比如实现自己的通道将消息发送到进程中。
为了方便这个用例,我们创建了两个模块:
- socket.io-redis
- socket.io-emitter
Using multiple nodes
Sticky load balancing 粘性负载平衡
If you plan to distribute the load of connections among different processes or machines, you have to make sure that requests associated with a particular session id connect to the process that originated them.
This is due to certain transports like XHR Polling or JSONP Polling relying on firing several requests during the lifetime of the “socket”. Failing to enable sticky balancing will result in the dreaded:
Error during WebSocket handshake: Unexpected response code: 400
如果计划在不同进程或计算机之间分配连接负载,则必须确保与特定会话ID关联的请求连接到发起它们的进程。
这是由于某些传输,如XHR轮询或JSONP轮询,依赖于在“套接字”的生命周期内触发多个请求。未能启用粘性平衡将导致可怕的
To achieve sticky-session, there are two main solutions:
- routing clients based on their originating address
- routing clients based on a cookie
要实现粘性会话,有两个主要解决方案:
- 根据客户端的原始地址路由客户端
- 基于cookie路由客户端
NginX configuration NginX配置
Within the http { } section of your nginx.conf file, you can declare a upstream section with a list of Socket.IO process you want to balance load between:
在nginx.conf文件的http {}部分中,您可以声明一个上游部分,其中包含要在以下之间平衡负载的Socket.IO进程列表:
http { server { listen 3000; server_name io.yourhost.com; location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $host; proxy_pass http://nodes; # enable WebSockets proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } upstream nodes { # enable sticky session based on IP ip_hash; server app01:3000; server app02:3000; server app03:3000; } }
Notice the ip_hash instruction that indicates the connections will be sticky.
请注意ip_hash指令,指示连接将是粘滞的。