Socket.io - 401-advanced-javascript-davetrost/alchemy-fsjs-fall-2019 GitHub Wiki

Socket.io Readings

Reading Materials

Socket.IO tutorial

  • Socket.IO really shines in RTAs (Real Time Applications). Examples of RTAs are: Instant messengers, push notifications (like Facebook notifications when someone tags you in a picture), collaboration apps (like Google docs with multi-edit capability), online gaming.
  • The http tool in socket.io can be used to attach a socket.io instance to an http server. The .on() method handles connection, disconnection, etc., events in it, using the socket object. It looks like the socket object is provided to the io.on callback function automatically. I assume common practice is to name this incoming parameter "socket"
//Whenever someone connects this gets executed
io.on('connection', function(socket) {
   console.log('A user connected');

   //Whenever someone disconnects this piece of code executed
   socket.on('disconnect', function () {
      console.log('A user disconnected');
   });
});

Socket.io vs Web Sockets

  • TLDR: Socket.IO leverages Web Sockets in its implementation. It's a higher level tool and addresses problems in the same domain space. They are inherently separate.

Getting Started

  • Note that Socket.IO is not a WebSocket implementation. Socket.IO uses WebSocket as a transport in some cases, but it adds more information. So, a WebSocket client will not connect to a Socket.IO server. And vice-versa, a Socket.IO client will not connect to a WebSocket server.
  • This web page includes examples of integrating socket.IO with various packages and tools, including: node & express.