├── test-client ├── client.js └── client.html ├── app.js └── README.md /test-client/client.js: -------------------------------------------------------------------------------- 1 | var app = require('http').createServer(handler) 2 | , io = require('socket.io').listen(app) 3 | , fs = require('fs') 4 | 5 | app.listen(8000); 6 | 7 | function handler (req, res) { 8 | fs.readFile(__dirname + '/client.html', 9 | function (err, data) { 10 | if (err) { 11 | res.writeHead(500); 12 | return res.end('Error loading index.html'); 13 | } 14 | 15 | res.writeHead(200); 16 | res.end(data); 17 | }); 18 | } -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var io = require('socket.io').listen(8001); 2 | var redis = require("redis"); 3 | 4 | io.of('/notifications'). 5 | on('connection', function (socket) { 6 | 7 | console.log("New connection: " + socket.id); 8 | // Send the message of connection for receiving the user ID 9 | socket.emit('connected'); 10 | 11 | // Receive the ID 12 | socket.on('join', function(userId){ 13 | var channel = "push:notifications:" + userId; 14 | console.log("Connecting to redis: " + channel); 15 | 16 | // store in the socket our connection 17 | socket.redisClient = redis.createClient(); 18 | socket.redisClient.subscribe(channel); 19 | 20 | // subscribe to our channel (We don't need to check because we have a 21 | // connection per channel/user) 22 | socket.redisClient.on("message", function(channel, message) { 23 | console.log(channel + ': ' + message); 24 | socket.emit('notification', channel, message); 25 | }); 26 | 27 | }); 28 | 29 | }); 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Realtime (Push) notifications based on Redis pub/sub, node.js and socket.io 2 | =========================================================================== 3 | 4 | The aim of this example is to provide a small guide for implementing 5 | realtime notifications based on redis and socket.io (with node.js) 6 | 7 | Schema 8 | ------ 9 | 10 | Client that pushes notifications -> Redis -> Node.js/Socket.io -> User browser 11 | 12 | 13 | Requirements 14 | ------------ 15 | 16 | - [Node](http://nodejs.org/) 17 | - [Redis](http://redis.io) 18 | 19 | Running 20 | ------- 21 | 22 | In the project folder install [socket.io](http://socket.io/) and node [redis client](https://github.com/mranney/node_redis): 23 | 24 | npm install socket.io redis 25 | 26 | Start the redis database: 27 | 28 | $ redis-server 29 | 30 | Start the push notifications server: 31 | 32 | $ node ./app.js 33 | 34 | Start the client node server: 35 | 36 | $ cd ./test-client 37 | $ node ./client.js 38 | 39 | Open a browser and go to 127.0.0.1:8000, enter the id that you want and we are 40 | ready to push some notifications. 41 | 42 | Open a redis client: 43 | 44 | $ redis-cli 45 | 46 | And with this key **push:notifications:[ID]** we publish messages into redis. 47 | 48 | $ PUBLISH "push:notifications:slok" "Justin Bieber sucks" 49 | 50 | So all the published messages in the appropiate redis channels, will be 51 | pushed to the user browser in realtime :) 52 | 53 | 54 | Now we should see the message **Justin Bieber sucks** in the message list 55 | 56 | Author 57 | ------ 58 | [Xabier Larrakoetxea](http://xlarrakoetxea.org) 59 | 60 | License 61 | ------- 62 | [GPL V3](http://www.gnu.org/licenses/gpl-3.0.html) 63 | 64 | 65 | -------------------------------------------------------------------------------- /test-client/client.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 37 | 38 | 39 |
40 | User ID:
41 | 42 |
43 |

Messages from server:

44 | 46 | 47 | --------------------------------------------------------------------------------