Websocket is a relatively simple protocol, there are many implementation versions in various languages, in fact, they are not very different, are based on webSocket to do some encapsulation, just choose one.

Socket. IO is one of many Websocket libraries. It does not simply implement Websocket like other libraries, but wraps a thick layer around websocket. Socket. IO defines a webSocket-based protocol, so the server and client of socket. IO must be compatible. In short, if the server uses socket. IO, the client has no choice but to use the socket. IO client as well.

IO server is based on node implementation (only provides this language server implementation), at the same time it provides a variety of language clients, including JavaScript, Java, C++, Swift, Dart, etc., we can see that socket. IO is for mobile terminal support a variety of languages. Socket. IO has a lofty goal: to be the best websocket library in the world.

Php-cli runs almost as efficiently as Node, but php-CLI does not perform as well as Node in terms of IO and processes. Socket. IO transfers json data. Working with JSON in JavaScript is a great experience.

For business and cost reasons, you might as well just use Golang instead of swoole, the market’s bluster. Swoole has its benefits, too, and is a good way to speed up your yii2 Laravel program. Using socket. IO socket services is a good solution if performance is acceptable.

How to install those who go to baidu or directly here www.shuaihua.cc/article/soc…

So let’s get straight to the two important apis

On (), like jquery, is used to bind events and emit() is used to bind the service that sends the event httpServer (http.server).Copy the code

Socket. IO several important events

Connect Client socket connection instance Connection is used the same as Event: 'connect'. Disconnect closes the connection to the client if the value of close istrue, the downlink is closed; otherwise, only the namespace is closed.Copy the code

Integrate in Express

var app = require('express') (); Var server = require('http').Server(app); Var IO = require('socket.io')(server); // Inject socket.io server.listen(80); app.get('/'.function (req, res) {
    res.sendfile(__dirname + '/index.html');
});

io.on('connection'.function(socket) {// When the link is triggered // socket client is currently sokcet pair socket.emit('news', {hello: 'world'});
    socket.on('my other event'.function (data) {
        console.log(data);
    });
});
Copy the code

Client code

<! DOCTYPE html> <html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
</html>
<script src="https://cdn.bootcss.com/socket.io/2.2.0/socket.io.js"></script>
<script>
    var socket = io.connect('http://localhost');
    socket.on('news'.function (data) {
        console.log(data);
        socket.emit('my other event', { my: 'data' });
    });
</script>

Copy the code

How do I send messages to specified users?

var app = require('express') (); Var server = require('http').Server(app); Var IO = require('socket.io')(server); // Inject socket.io server.listen(80); app.get('/'.function (req, res) {
    res.sendfile(__dirname + '/index.html');
});


letSocketObj = {}; SocketObj IO. On (SocketObj IO.'connection'.function(socket) {// SocketObj[socket.id] = socket; // Socket client is currently socket.emit('news', {hello: 'world'});
    socket.on('my other event'.function (data) {
        console.log(data);
    });

    io.sockets.on('private message'.function (data) {
        letto = data.from; SocketObj[to].emit('notice email', {data: data.msg});
        //SokcetObj[to].emit('notice message', {data: data.msg}); //SokcetObj[to].emit('notice msg', {data: data.msg}); }) socket.on('disconnect'.function () {
        delete SocketObj[socket.id];
        socket.emit('notice disconnect'); // Notify that someone has left})});Copy the code

Notice when to use io.on() and when to use socket.on()

How to make a specific chat room

Join (room[, callback]) IO. On ('connection', (socket) => {
  socket.join('room 237', () = > {let rooms = Objects.keys(socket.rooms);
    console.log(rooms); // [ <socket.id>, 'room 237' ]
    io.to('room 237'.'a new user has joined the room'); // broadcast to everyone inthe room }); }); Socket.leave (room[, callback]) removes the client from the specified room and optionally executes an exception callback function. [socket.to](room) In order to trigger the same broadcast for multiple rooms, you need to chain to multiple rooms several times.Copy the code