Example code for Koa Socket instant messaging

HTTP is characterized by a question and answer, and instant messaging is required for two-way communication, so that the previous instant communication can only be passed by the polling. Periodic AJAX requests to obtain data until WebSocket appears, perfectly realize two-way communication

Analog communication mode

segment polling
The front desk uses setInterval to perform the background, which is undoubtedly very wasteful

Long polling and long connection (HTML5 EventSource)

The customer service is connected once. The server is constantly open. The server is sent to the front desk. The customer service side and the server remains always connected. The disadvantage is that only the server outputs to the customer service.


WebSocket is no longer a http protocol, but is upgraded to a WebSocket protocol, saying that it is to take the application layer protocol (TCP) To achieve two-way communication, the disadvantage is the compatibility problem (IE11)

There is a certain problem in the above manner, naturally Someone packages a full-purpose solution, Socket.IO is another solution to the above solution

Two instant communication realization

Customer Service

The server side is interacting with ON and EMIT

EMIT indicates that the transmission, event name is customized, the other end is used to receive

ON to accept, similar JQBinding event, event name corresponds to EMIT event name Connect, disconnect is a connection and disconnection event

    // Establish connection VAR socket = IO (‘http: // localhost: 80’) // ON indicates that the reception // EMIT indicates sends socket.on (‘connect’, function () {console.log (‘connection is “) // login, before and after synchronization End information / / Request the backend login interface, write socketid socket.emit (‘login “, {// identity, can be a timestamp or unique ID, the most used to go back to SocketID for private chat ID: username})} ) // Send a message, here you can send Socket.emit (‘sendmsg’, {newaccount: ‘xxxxxx’}) // receive message socket.on (‘msg1’, function (data) {// can be used to send Socket.emit (‘SENDMSG’, {newaccount: ‘xxxxxx’}) // Receive Message Socket.on (‘MSG1’) {// Rendering the data (‘allMessage’, Function (DATA) {// receives all people message console.log (data);}) // indicates that the connection is disconnected. Socket.on (‘disconnect’, function () {console.log (‘chat server is broken “})
Install KOA-Socket
   NPM I KOA2KOA-Socket -S 

Const IO = Require (‘Koa-Socket’) Const Koa = Require (‘KOA2’) Const IO = new io () const app = new koa () // Connect the socket and Application IO.attach (App) // and the customer service end to IO.on (‘connection’, (context) => {Console.log (‘Connection “}) // Receive message IO.ON (‘ sendmsg ‘, function (context) {//console.log (tet.data) // Send message to customer service to the customer service Time to send message IO.Broadcast (‘ msg1 ‘,’ I am the server ‘) // broadcast, all people message IO.Broadcast (‘ allmessage ‘, context.data.newaccount)} // Processing login sync information o.n (‘ login ‘, context => {let id = context.data.id; console.log (context);};


 The above is a brief introduction of Koa-Socket, which only implements simple messaging and reception. There is also private chat and group, whose principle is to get the socketID of each user, send a message, send a message, then update again   
The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Clouds.
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment