Monday, January 2, 2017

Trying to explain and understand "A simple chat server using Socket.IO, Express, and Async" - PART I

  //
  // # SimpleServer
  //
  // A simple chat server using Socket.IO, Express, and Async.
  //
var http = require('http');  //OK, to use Node's HTTP server and client, one must require('http') from the get-go
var path = require('path');  // OK, there is a lot that goes into using and manipulating file and directory paths; this provides a set of utilities to do that

var async = require('async'); // OK, this provides a whole host of powerful functions for working with asynchronous JavaScript
var socketio = require('socket.io'); // OK, WebSockets facilitates ongoing conversation between browser and web server without being solicited by the client,  while keeping the connection open.

var express = require('express');  // OK, just a bunch of additional web application tools inside of Node

  //
  // ## SimpleServer `SimpleServer(obj)`
  //
  // Creates a new instance of SimpleServer with the following options:
  //  * `port` - The HTTP port to listen on. If `process.env.PORT` is set, _it overrides this value_.
  //
var router = express();
      // OK, first of all, "routing." What is routing in node? Here is the basic format: app.method(path, handler) (see https://expressjs.com/en/starter/basic-routing.html )
        //"app" is an instance of express (in this example, the app is called 'router' since 'var router = express()')
        //"method" is one of several HTTP request methods (get, post, put, delete, etc)
        // "path" is a path on the server to a particular resource
        // "handler" is whatever function is to be carried out when Node finds a match for the route
      // SO, what's going on here is you are saying the "app" part of 'app.method(path, handler)' is going to be called 'router'

var server = http.createServer(router); // OK, this calls http.Server() internally and returns the resulting instance

var io = socketio.listen(server); // OK, this marks the socket referred to as a passive socket that will be used to accept incoming connection requests  

router.use(express.static(path.resolve(__dirname, 'client')));  // OK you are passing the name of the directory that contains the static assets to the express.static middleware function


var messages = []; // OK, this is just creating an array called messages
var sockets = []; // OK, this is just creating an array called sockets

io.on('connection', function (socket) {
    messages.forEach(function (data) {
      socket.emit('message', data);
    });

    sockets.push(socket);

    socket.on('disconnect', function () {
      sockets.splice(sockets.indexOf(socket), 1);
      updateRoster();
    });

    socket.on('message', function (msg) {
      var text = String(msg || '');

      if (!text)
        return;

      socket.get('name', function (err, name) {
        var data = {
          name: name,
          text: text
        };

        broadcast('message', data);
        messages.push(data);
      });
    });

    socket.on('identify', function (name) {
      socket.set('name', String(name || 'Anonymous'), function (err) {
        updateRoster();
      });
    });
  });

function updateRoster() {
  async.map(
    sockets,
    function (socket, callback) {
      socket.get('name', callback);
    },
    function (err, names) {
      broadcast('roster', names);
    }
  );
}

function broadcast(event, data) {
  sockets.forEach(function (socket) {
    socket.emit(event, data);
  });
}

server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
  var addr = server.address();
  console.log("Chat server listening at", addr.address + ":" + addr.port);
});

No comments:

Post a Comment