O'Reilly logo

Socket.IO Cookbook by Tyson Cadenhead

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Real-time analytics

Socket.IO excels at creating rich real-time analytic dashboards. In this recipe, we will display the count of users currently on our page, but the same concept could be used to show much more detailed analytical data if it is provided.

How to do it…

To show a real-time count of the users currently on a page, follow these steps:

  1. Create a server.js file that emits the count of active users on the page whenever the count changes. Take a look at the following code:
    var express = require('express'), app = express(), http = require('http'), socketIO = require('socket.io'), server, io; app.get('/', function (req, res) { res.sendFile(__dirname + '/index.html'); }); server = http.Server(app); server.listen(5000); io = socketIO(server); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required