socket.io II: Basic Applications

Posted by JeanieTallis on Sun, 07 Jul 2019 21:01:02 +0200

socket.io provides event-based real-time two-way communication

Connection between server and client

socket.io also provides API s for both the server and the client

Server socket.io must bind an instance of http.Server

Binding http.Server

  1. Implicit binding

Implicit binding is done by calling listen or attach functions when an incoming port is instantiated or after instantiation. Instantiate and listen for http.Server inside socket.io

  • Incoming port at instantiation

let io = require('socket.io')(3000)
  • Binding directly through listen or attach functions. Listen and attach

let io = require('socket.io')
io.listen(3000) // io.attach(3000) 
  1. Display Binding

You can specify http.Server manually

  • Binding at instantiation

let server = require('http').Server();
let io = require('socket.io')(server)

server.listen(3000)
  • Binding by listen or attach

let server = require('http').Server();
let io = require('socket.io')()

io.listen(server) // io.attach(server)

server.listen(3000)
  • http frameworks such as express or koa can be bound

express

let app = require('express')
let server = require('http').Server(app)
let io = require('socket.io')(server)

app.listen(3000)

koa

let app = require('koa')()
let server = require('http').Server(app.callback())

let io = require('socket.io')(server)

app.listen(3000)

Monitor connection status

When the connection between the server and the client is successful, the server monitors connection and connection events (connection and connection are synonymous), the client monitors connection events, and when disconnection occurs, both the socket and the client that correspond to the client will listen to disconnect events.

Server-side code

let server = require('http').Server()
let io = require('socket.io')(server)

server.listen(3000);
io.on('connection', socket => {
  console.log('connect')
  socket.on('disconnect', () => {
    console.log('disconnect')
  })
  socket.disconnect()
})

Printing after Operation

connect
disconnect

Client Code

let socket = io('http://localhost:3000')
socket.on('connect', () => {
  console.log('connect')
})
socket.on('disconnect', () => {
  console.log('disconnect')
})

Printing after Operation

connect
disconnect

Data transmission

The socket of server and client is an associated EventEmitter object. Events sent by client socket can be received by server socket, and events sent by server socket can also be accepted by client. Based on this mechanism, two-way communication can be achieved.

Now we simulate a situation where the client sends random numbers continuously. When the random number is greater than 0.95, the server delays 1 s and sends warnings and warnings to the client.

Server-side code

let server = require('http').Server()
let io = require('socket.io')(server)

server.listen(3000);
io.on('connection', socket => {
  socket.on('random', value => {
    console.log(value)
    if (value > 0.95) {
      if (typeof socket.warning === 'undefined') socket.warning = 0
      setTimeout(() => {
        socket.emit('warn', ++socket.warning)
      }, 1000)
    }
  })
})

Socket objects can be used to store state information and custom data, such as socket.warning

Client Code

let socket = io('http://localhost:3000')
let interval = setInterval(() => {
  socket.emit('random', Math.random())
}, 500)
socket.on('warn', count => {
  console.log('warning count: ' + count)
})
socket.on('disconnect', () => {
  clearInterval(interval)
})

Transport stream

socket.io can handle streams

Server-side code

io.on('connection', function (socket) {
  let stream = ss.createStream()
  ss(socket).emit('script', stream)
  fs.createReadStream(__filename).pipe(stream)
})

Client Code

let socket = io('http://localhost:3000') 
ss(socket).on('script', stream => {
  let buffer = '' 
  stream.on('data', data => {
    buffer += data.toString()
  })
  stream.on('end', () => {
    console.log(buffer)
  })
})

More articles are listed in the Here

Topics: Javascript socket