Node. js Socket.IO advanced use skills

  • 2020-03-30 04:14:43
  • OfStack

In my last post (link: #), I briefly introduced the basics of socket.io and created a simple chat room DEMO. Based on the introduction, this article continues to explore the advanced use of socket.io. This article will introduce some useful apis and considerations in socket.io from the aspects of configuration, room, and events.

1. The configuration

Socket.io provides four configured apis: IO. Configure, IO. Set, IO. Enable, and IO. Disable. Among them, IO. Set sets the item, and IO. Enable and IO. Disable are used for the item setting Boolean configuration. Io.configure lets you configure different parameters for different production environments (devlopment, test, etc.). The following defines the different configurations of socket.io in the development and release environments:


var io = require('socket.io').listen(80); io.configure('development', function(){
    io.enable('browser client etag');
    io.set('log level', 1);
}); io.configure('release', function(){
    io.set('transports', ['websocket']);
});

Some common configuration items are listed below. See the official WIKI for configuration parameters

Room 2.

The room is a very useful feature of socket.io. The room provides a namespace for some of the specified clients, and all broadcasts and communications in the room do not affect the clients outside the room.

In the introductory section, we learned that socket.join('room name') is used for clients to enter the room and socket.leave('room name') is used to leave the room. When a client enters a room, it can broadcast messages in the room in two ways:


//1. Broadcast an event to my room, and the submitter will be excluded (i.e., no message will be received)
io.sockets.on('connection', function (socket) {
    //Note: in contrast to the following, the event
is submitted from the client's perspective     socket.broadcast.to('my room').emit('event_name', data);
} //2. Broadcast an event to the other room, where all clients receive a message
//Note: in contrast to the above, the event
is submitted from the server's perspective io.sockets.in('another room').emit('event_name', data); //Broadcast
to all clients io.sockets.emit('event_name', data);

In addition to broadcasting messages to the room, you can also get information about the room through the following API.


//Get all room information
//The key is the room name, and the value is the socket ID array corresponding to the room name
io.sockets.manager.rooms //Gets the client in the particular room and returns all socket instances in that room
io.sockets.clients('particular room') //
to get the room information of this socket through socket.id io.sockets.manager.roomClients[socket.id]

3. The event

Socket.io has some default events built in. We should avoid the default event names when designing events, and use these default events flexibly.

Server-side events:

1). IO. Sockets. On ('connection', function(socket) {}) : triggered after successful socket connection, used for initialization
Socket.on ('message', function(message, callback) {}) : this event is triggered when the client sends a message via socket.send
2). Socket.on ('anything', function(data) {}) : triggered when any event is received
3). Socket. On ('disconnect', function() {}) : triggered when the socket loses connection (including closing the browser, actively disconnecting, disconnecting, etc.)

Client events:

1). Connect
2). Connecting
3) disconnect
4). Connect_failed: connection failed
5). Error: error occurs and cannot be handled by other event types
6). Message: same server-side message event
7). Anything: same server-side anything event
8). Reconnect_failed: reconnection failed
9). Reconnect successfully
10). Reconnecting

Here we need to mention the order in which the client socket initiates the connection. When the first connection occurs, the events are triggered in the following order: connecting-> The connect. When a connection is lost, the event sequence is: disconnect-> Reconnecting (possibly multiple times) -> Connecting - > Reconnect - > The connect.

Authorized by 4.

1). Broadcast to all clients: socket.broadcast. Emit ('broadcast message');

2). Getting into a room (great! Socket.join ('your room name') : socket.join('your room name')

3). Broadcast a message to a room (the sender cannot receive the message) : socket.broadcast. To ('your room name'). Emit ('broadcast room message');

4). Broadcast a message to a room (this API belongs to IO. Sockets) : IO. Sockets. In ('another room name'). Emit ('broadcast room message');

5). Force the use of WebSocket communication (client) socket.send('hi'), (server) with socket.on('message', function(data){}) to receive.

So much for the advanced usage of socket.io. I feel that these basic apis are sufficient for everyday use, which reflects the philosophy of socket.io, which is extremely simple and easy to use. This article is just a primer, and if you have a problem that you can't solve in practice, it's a good idea to check out the official, detailed WIKI.


Related articles: