Headless client connection to Socket.io and authenticated with Passport

Now that I have a working HMI setup with user authentication, I want to extend it to include remote devices and computers. The easiest way to connect remote computers is to emulate the browser connection. Since malicious hackers will probably attempt to do this anyway it is also a great way to test the security and robustness of the system. My example is taken directly from the creator of Passport.SocketIO, José F. Romaniello, and can be found here https://gist.github.com/jfromaniello/4087861. In my case socket.io is authenticated by using the cookie session stored when the user logs in to express. This is a headless instance, so the cookie needs to be requested and stored in the client side program. Fortunately the gist provided by José F. Romaniello does just that.

The only change I needed to make was to call the xmlhttprequest installed with socket.io-client.

require('socket.io-client/node_modules/xmlhttprequest').XMLHttpRequest = function(){

The heart of the program is with the socket.on section. This is the place to add the socket.emit() and socket.on() functions.

  var socket = io.connect('http://127.0.0.1:8080');
  socket.on('connect', function(){
    console.log('connected! handshakedddddddddddd')
    //send socket events to close a valve
socket.emit('closecmd', 'valve1001');
//wait and send socket event to open a valve
    setTimeout(function () {
socket.emit('opencmd', 'valve1001');
}, 2000)
  });

Read More