rabbit.js icon indicating copy to clipboard operation
rabbit.js copied to clipboard

Managing offline messages

Open m-frachet opened this issue 10 years ago • 1 comments

I didn't know where to ask this, and cant find it in the README or exemple, so I try there.

Is it possible, using RabbitJS, to manage offline messages when a user disconnects ?

The aim is to use it for Android Application and SocketIO even if there are network loss.

Can you tell me how should I do that ?

Here's my code :

var context = require('rabbit.js').createContext(); var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server);

app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); });

server.listen(8888);

io.sockets.on('connection', function (socket) { var pub = context.socket('PUB'); var sub = context.socket('SUB'); sub.setEncoding('utf8'); pub.connect('q'); sub.connect('q');

socket.on('message', function (msg) {
    pub.write(msg, 'utf8');
});

sub.on('data', function (msg) {
    socket.emit('news', msg);
});

/* On ferme les streams lorsqu'on se deco */
socket.on('disconnect', function () {
    pub.close();
    sub.close();
});

});

m-frachet avatar Mar 04 '15 09:03 m-frachet

This is not really encouraged, by design -- PUB/SUB sockets are meant to only collect messages while your application is running.

You may find it easier to encode what you want in amqplib (squaremo/amqp.node), which has full control over the lifetime of exchanges and queues and so on, at the cost of having to do things at a "lower level".

squaremo avatar Mar 14 '15 16:03 squaremo