Socket.IO - evan-401-advanced-javascript/seattle-javascript-401d31 GitHub Wiki
Socket.IO is a a javascript library that allows for real time communication between web applications. To enable this real time communication, socket.IO uses 2 different parts, a client side and a server-side library. A test web app can be run simply with nodemon using the 2 files below to create a web app with an automatic 4 second timeout using socket.IO
index.html file: `
<title>Hello world</title> <script src = "/socket.io/socket.io.js"></script> <script> var socket = io(); socket.on('testerEvent', function(data){document.write(data.description)}); </script> Hello world `app.js file: `var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http);
app.get('/', function(req, res) { res.sendfile('index.html'); });
io.on('connection', function(socket) { console.log('A user connected');
//Send a message when setTimeout(function() { //Sending an object when emmiting an event socket.emit('testerEvent', { description: 'A custom event named testerEvent!'}); }, 4000);
socket.on('disconnect', function () { console.log('A user disconnected'); }); });
http.listen(3001, function() { console.log('listening on *:3001'); });`