Simple node WAMP with redis pubsub

Tried to play with node. It’s always interesting to try something new.

My basic problem was to get new updates from redis queue using subscribe to channel. And pushing this message to client via websockets. I found and decide to use is as example.

Everything was brand new for me so today my story will be quite detailed.

Ok let’s go step by step.

1. Initiate our new project

  • mkdir wamptest
  • npm init (follow instructions)

2 Then as I mentioned I need some 3rd party libraries. Let’s add them

  • npm install redis –save
  • npm install –save

Here’s what I have in package.json

  "name": "radzserg-wamp-play",
  "description": "WAMP server toy project",
  "version": "0.0.1",
  "main": "server.js",
  "dependencies": {
    "redis": "^0.10.3",
    "": "0.8.x"
  "private": "true"

Let’s finally start coding

We will use 2 files. Client and server. Client will be simple


Basically we set up socket connection and will just show what we got from server.

    <script src=""></script>
    <script src="/"></script>
<!-- And yes we don't need to create this file. Node will do that for us. Some kind of magic that I didn't get at the beginning and was completely confused -->
        $(function () {
            var socket = io.connect(null, {
                query: 'token=' + 123 + '&channelId=' + 1
            socket.on('connect', function () {
                socket.on('message', function (message) {
                socket.on('disconnect', function () {
<ul id="messages"></ul>

And the server.js

var fs = require('fs'),
    http = require('http'),
    redis = require("redis"),
    sio = require('');

var server = http.createServer(function (req, res) {
    res.writeHead(200, { 'Content-type': 'text/html'});
server.listen(8000, function () {
    console.log('Server listening at http://localhost:8000/');

// Attach the server
io = sio.listen(server);

var chanelId = null
io.set('authorization', function (data, accept) {
    if (!data.query.token) {
        return accept('No token transmitted.', false);
    if (!data.query.chanelId) {
        return accept('No chanelId transmitted.', false);
    var token = data.query.token;
    chanelId = data.query.chanelId;
    // @todo authorize via token
    // to be continued ))
    accept(null, true);

var client = redis.createClient();

// Define a message handler
io.sockets.on('connection', function (socket) {

    client.subscribe("channel." + chanelId);
    client.on("message", function (channel, message) {
        //message = JSON.parse(message)

        // just catch and emit to the client
        console.log(channel + ": " + message);
        socket.broadcast.emit('message', message);


For publishing events
For checking the queue redis-cli > monitor
Run the our server node server.js
Here’s what I eventually got


One thought on “Simple node WAMP with redis pubsub

Leave a Reply

Your email address will not be published. Required fields are marked *