
Step #1: Project initialization
The first goal is to set up a simple HTML webpage that serves out a form and a list of messages. We’re going to use the Node.JS web framework express to this end. Make sure Node.JS is installed.
First let’s create a package.json manifest file that describes our project. I recommend you place it in a dedicated empty directory (I’ll call mine socket-chat-example).
Step #2: Serving HTML
So far in index.js we’re calling res.send and passing it a string of HTML. Our code would look very confusing if we just placed our entire application’s HTML there, so instead we're going to create a index.html file and serve that instead.
Step #3: Integrating Socket.IO
Socket.IO is composed of two parts:
A server that integrates with (or mounts on) the Node.JS HTTP Server (the socket.io package)
A client library that loads on the browser side (the socket.io-client package)
During development, socket.io serves the client automatically for us, as we’ll see, so for now we only have to install one module:
Step #5: Broadcasting
The next goal is for us to emit the event from the server to the rest of the users.
In order to send an event to everyone, Socket.IO gives us the io.emit() method.
Step #4: Emitting events
The main idea behind Socket.IO is that you can send and receive any events you want, with any data you want. Any objects that can be encoded as JSON will do, and binary data is supported too.
Let’s make it so that when the user types in a message, the server gets it as a chat message event. The script section in index.html should now look as follows:
Lesson Title: Introduction to Socket IO
Objective: By the end of this lesson, students will understand the basics of Socket IO and its significance in real-time web applications.
Socket IO is an open-source, cross-platform library that provides full-duplex bidirectional communication between a client and a server based on events. It is built on the WebSocket protocol, providing additional capabilities such as automatic reconnection and fallback to HTTP long polling where WebSockets cannot be used.
Automatic reconnection
Under some particular conditions, the WebSocket connection between the server and the client can be interrupted with both sides being unaware of the broken state of the link.
That's why Socket IO includes a heartbeat mechanism, which periodically checks the status of the connection.
And when the client eventually gets disconnected, it automatically reconnects with an exponential back-off delay, in order not to overwhelm the server.
Packet buffering
The packets are automatically buffered when the client is disconnected, and will be sent upon reconnection.
More information
How it works
The bidirectional channel between the Socket IO server (Node js) and the Socket IO client (browser, Node js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.
Materials:
- A computer with Node.js installed
- Internet access
- Code editor (e.g., Visual Studio Code)
- Node js
- Javascript