Class 32 Reading: Socket.IO Client with React - mwilkin-401-advanced-javascript/bend-javascript-401d2 GitHub Wiki

Why Socket.IO?

Real-time event responsiveness:

* The server will stream data down to the client as it becomes available; no web page refreshing necessary
* Not relying on the client to decide when to get data from the server.

Socket.io creates a WebSocket service quickly which are duplex channels between the client and server.

  • WebSockets have the ability to work with an event-driven model: both the server and the client can react to events and messages.
  • The most important thing to do on the server is to handle a connection of a client, so that events can be emitted (published) to that client.

Socket.IO is not a WebSocket implementation

* “Socket.IO indeed uses WebSocket as a transport when possible but a WebSocket client will not be able to connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server” -authors of Socket.IO

Most important method while working with Socket.IO is on().

Takes two arguments:

  1. the name of the event
  2. A callback which will be executed after every connection event.

The connection event returns a socket object which will be passed to the callback function. Using this socket will enable data to be sent back to a client in real time.