Socket.IO client events

A client doesn't necessarily need to be a web browser. We could write a Node.js Socket.IO client application as well.

The Socket.IO client events are extensive and give a lot of control over your application:

  • connect: This event gets fired when there is a successful connection to the server
      clientSocket.on('connect', () => { 
          console.log('Successfully connected to server') 
      }) 
  • connect_error: This event is emitted when there is an error when trying to connect or reconnect to the server
      clientSocket.on('connect_error', (error) => { 
          console.log('Connection error:', error) 
      }) 
  • connect_timeout: By default, the timeout set before a connect_error and connect_timeout is emitted is 20 seconds. After this, the Socket.IO client may try to reconnect to the server once again:
      clientSocket.on('connect_timeout', (timeout) => { 
          console.log('Connect attempt timed out after', timeout) 
      }) 
  • disconnect: This event is fired when the client is disconnected from the server. An argument is provided specifying the reason of the disconnection:
      clientSocket.on('disconnect', (reason) => { 
          console.log('Disconnected because', reason) 
      }) 
  • reconnect: Fired after a successful reconnection attempt. An argument is provided that specifies how many attempts happened before the connection was successful:
      clientSocket.on('reconnect', (n) => { 
          console.log('Reconnected after', n, 'attempt(s)') 
      }) 
  • reconnect_attempt or reconnecting: This event is emitted when trying to reconnect to the server. An argument is provided specifying the number of current attempts to connect to the server:
      clientSocket.on('reconnect_attempt', (n) => { 
          console.log('Trying to reconnect again', n, 'time(s)') 
      })  
  • reconnect_error: Similar to the connect_error event. However, it gets fired only if there is an error when trying to reconnect to the server:
      clientSocket.on('reconnect_error', (error) => { 
          console.log('Oh no, couldn't reconnect!', error) 
      })  
  • reconnect_failed: By the default, the maximum number of attempts is set to Infinity. That means, it is unlikely that this event will ever get fired. However, we can specify an option to limit the maximum number of connection attempts. Let's see that later:
      clientSocket.on('reconnect_failed', (n) => { 
    console.log('Couldn'nt reconnected after', n, 'times') 
      }) 
  • ping: In short, this event gets fired to check if the connection with the server is still alive:
      clientSocket.on('ping', () => { 
          console.log('Checking if server is alive') 
      }) 
  • pong: Fired when a response is received from the server after the event ping is fired. An argument is provided specifying the latency or response time:
      clientSocket.on('pong', (latency) => { 
          console.log('Server responded after', latency, 'ms') 
      }) 
  • error: This event is fired when an error occurs within events:
      clientSocket.on('error', (error) => { 
          console.log('Oh no!', error.message) 
      }) 
  • [eventName]: A user-defined event that gets fired when the event is emitted in the server. The arguments provided by the server will be received by the client.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.90.140