In depth Multiplayer - abukhalil-LTUC-ASAC/amman-401d4 GitHub Wiki

Actually just more about socket.io

Simple because old stacks used to track and broadcast changes through traditional search and track with timestamps and database changes, what websockets offer are magical TCP super fast way of doing bi-directional communication where both client and server could speak to each other at the same time.

Despite that, http service is still needed not exactly as a fallback for servers who cannot use it due to hardware or software limitations, but because socket.io library is extensive and tries to support other form of data transfer such as JSONP, and sometimes these services are exclusive to websockets which is simply an overkill to have websocket work that instead.

How emitters work?

Client emits one way, to the server where it would be processed, server on the other hand could emit to all, or choose a category of rooms, or even finely define each client on their own if needed, these rooms can be defined to host bunch of clients and each client could join and leave on its own without much coding involved, reminds me of old and ugly email services but on the backend, and anything is possible.

Clients sometimes miss an event when they disconnect unintentionally, there should be some cautionary code to ask the server to fire missed events again, this is possible to implement when the server expects some confirmation response but does not get it, so the event is stored until a queue to fire all missed events happens again with reconnection.

Http integration

Using express, it is possible to start socket.io simply by connecting it with:

var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);

app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
  console.log('a user connected');
});
http.listen(3000, () => {
  console.log('listening on *:3000');
});

and on the client side:

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io();
</script>

And now every time a refresh happens, user connected is displayed on console.

⚠️ **GitHub.com Fallback** ⚠️