Alexis
Alexis

Reputation: 25163

Redis to Socket.IO

I am making a real-time webapp using Django. Django does all the heavy lifting. To make the app real-time, when a change is made in Django, it is published to redis. I then set up a node.js app that marshals the data from redis to socket.io.

So right now things look like this.

Django -> Redis -> Node.js -> Socket.IO -> Browser

I'm OK with using redis and think it is great. I looked into gevent-socketio but it is not what I am looking for. Is there a way to do:

Django -> Redis -> Socket.IO -> Browser?

Upvotes: 2

Views: 627

Answers (3)

Nathan Romano
Nathan Romano

Reputation: 7096

We started a project called bus.io on npm. It should help you. It simplifies connecting sockets to a pub sub and distributing messages to them.

On the server.

var express = require('express')
var app = express();
app.use(express.static(__dirname+'/public'));

var server = require('http').Server(app).listen(3000);

var bus = require('bus.io')(server);

On the client.

<script type="text/javascript src="/bus.io/bus.io.js">
  var sock = io.connect();
  sock.on('connect', function () {
    sock.message().action('say').content('hello').deliver();
  });
  sock.on('say', function (msg) {
    console.log(msg.content());
  });
</script>

This example demonstrates building a message and delivering it to the server. By default the message will loop back to the sender. A "sender" is an actor and by default a sender will send a message to itself. You can customize the actor and target. So for instance you wanted to send a message to everyone.

On the server you could add this to force all your messages to go to everyone.

sock.in(function (msg, sock, next) {
  msg.target('everyone').deliver();
});

Or on the client.

sock.message().target('everyone').action('say').content('hello').deliver();

There is a shorthand too.

sock.message({target:'everyone', action:'say', content:'hello'}).deliver();

You can also change how the actor is assigned, by default it is the socket.id.

bus.actor(function (sock, cb) {
  //so if you had a session you could get the user name from the session
  cb(null, sock.handshake.session.user);
});

Messages are automatically propagated from the client to other clients through the bus. The bus is built ontop of redis and socket.io.

If you have any questions or would like some help contact checkout our http://turbonetix.io or the project on github https://github.com/turbonetix/bus.io.git

Upvotes: 1

luin
luin

Reputation: 1995

You can use django-socketio.

What you have done is similar to Trello:

http://blog.fogcreek.com/the-trello-tech-stack/

Upvotes: 1

mariodev
mariodev

Reputation: 15519

Redis and socket.io don't communicate directly.. you need server in between, especially if You use websockets, long-polling etc.

Upvotes: 4

Related Questions