├── README.md └── node.js ├── package.json ├── README.md ├── client.html └── server.js /README.md: -------------------------------------------------------------------------------- 1 | Realtime stats implementation using Node.js/Golang, Redis and Socket.IO 2 | 3 | - node.js : Node.js implementation 4 | - golang : Go implementation 5 | -------------------------------------------------------------------------------- /node.js/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "node.js", 3 | "description" : "Realtime stats using node.js, socket.io and redis", 4 | "version" : "0.0.1", 5 | "author": { 6 | "name": "Venu Anuganti", 7 | "email": "venu@venublog.com" 8 | }, 9 | "dependencies" : { 10 | "redis": "", 11 | "socket.io": "", 12 | "express": "" 13 | }, 14 | "repository" : { 15 | "type": "git", 16 | "url": "http://github.com/vanuganti/realtime" 17 | }, 18 | "engines" : { 19 | "node": ">=0.4.0" 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /node.js/README.md: -------------------------------------------------------------------------------- 1 | Realtime stats - Node.js, Socket.IO & Redis 2 | =========================================== 3 | 4 | For complete walk-through, check my blog post on this: http://venublog.com/2013/06/26/realtime-web-stats-using-node-js-socket-io-and-redis/ 5 | 6 | Requirements: 7 | ------------- 8 | - Node.js server (http://nodejs.org/) 9 | - Node.js modules 10 | - express (http://expressjs.com/) 11 | - redis (https://github.com/simplegeo/nodejs-redis) 12 | - socket.io (http://socket.io/#how-to-use) 13 | 14 | - Redis server (http://redis.io/) 15 | 16 | Get Started: 17 | ------------ 18 | 19 | - Clone the git repo and install dependency packages 20 | 21 | 22 | ```shell 23 | $ git clone git@github.com:vanuganti/realtime.git 24 | $ cd realtime 25 | $ cd node.js 26 | $ npm install 27 | ``` 28 | 29 | - Start the redis server (default localhost on port 6379) 30 | - Start node with server.js 31 | 32 | ```shell 33 | $ node server.js 34 | ``` 35 | 36 | - Open client.html in browser code 37 | 38 | ```shell 39 | $ open client.html 40 | ``` 41 | 42 | - Publish a counter to 'realtime' channel in redis 43 | 44 | ```shell 45 | $ redis-cli publish realtime 99 46 | ``` 47 | 48 | - Notice the updated counter in browser 49 | 50 | For additional details, check http://venublog.com/2013/06/26/realtime-web-stats-using-node-js-socket-io-and-redis/ 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /node.js/client.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | REAL TIME Node.js, Redis and Socket.IO 5 | 6 | 7 | 8 | 9 | 10 | 11 | 39 | 40 | 41 | 42 | 43 | 44 | 47 | 48 |
45 |

0

46 |
49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /node.js/server.js: -------------------------------------------------------------------------------- 1 | /* 2 | Node.js server script 3 | Required node packages: express, redis, socket.io 4 | */ 5 | const PORT = 3000; 6 | const HOST = 'localhost'; 7 | 8 | var express = require('express'), 9 | http = require('http'), 10 | server = http.createServer(app); 11 | 12 | var app = express(); 13 | 14 | const redis = require('redis'); 15 | const client = redis.createClient(); 16 | log('info', 'connected to redis server'); 17 | 18 | const io = require('socket.io'); 19 | 20 | if (!module.parent) { 21 | server.listen(PORT, HOST); 22 | const socket = io.listen(server); 23 | 24 | socket.on('connection', function(client) { 25 | const subscribe = redis.createClient() 26 | subscribe.subscribe('realtime'); 27 | 28 | subscribe.on("message", function(channel, message) { 29 | client.send(message); 30 | log('msg', "received from channel #" + channel + " : " + message); 31 | }); 32 | 33 | client.on('message', function(msg) { 34 | log('debug', msg); 35 | }); 36 | 37 | client.on('disconnect', function() { 38 | log('warn', 'disconnecting from redis'); 39 | subscribe.quit(); 40 | }); 41 | }); 42 | } 43 | 44 | function log(type, msg) { 45 | 46 | var color = '\u001b[0m', 47 | reset = '\u001b[0m'; 48 | 49 | switch(type) { 50 | case "info": 51 | color = '\u001b[36m'; 52 | break; 53 | case "warn": 54 | color = '\u001b[33m'; 55 | break; 56 | case "error": 57 | color = '\u001b[31m'; 58 | break; 59 | case "msg": 60 | color = '\u001b[34m'; 61 | break; 62 | default: 63 | color = '\u001b[0m' 64 | } 65 | 66 | console.log(color + ' ' + type + ' - ' + reset + msg); 67 | } --------------------------------------------------------------------------------