tonyg / presence-exchange

An experimental RabbitMQ "Presence" exchange: notifies bound queues when other bindings appear and disappear
Other
32 stars 15 forks source link

away #11

Closed vinnitu closed 6 years ago

vinnitu commented 7 years ago

can we add new type of presence action (i.e. "away") to set (bind/online, unbind/offline)?

tonyg commented 7 years ago

You can set an arbitrary binding key to indicate user status, if you like. One approach might be to bind once with a binding key of "online", and then a second time with a binding key of "away". When the user returns, delete the "away" binding. Then observers would see either:

vinnitu commented 7 years ago

hm... how to explicit set binding key to "online" status in this code snippet?

var amqp = require('amqplib/callback_api');

function on_connect(err, conn) {
  var ex = 'room';
  var id = 'jack';

  function on_channel_open(err, ch) {
    ch.assertExchange(ex, "x-presence", {durable: true}, function(err, ok) {
        ch.assertQueue("", {exclusive: true}, function(err, ok) {
            ch.bindQueue(ok.queue, ex, id);
        });
    });
  }

  conn.createChannel(on_channel_open);
}

amqp.connect(on_connect);
tonyg commented 7 years ago

I see. You are already using the binding key for the username.

Perhaps instead using a list of key-value pairs would work for you? It's not ideal, but it's what's there. Something like this:

ch.bindQueue(ok.queue, ex, 'user=' + id + ';status=' + status);

Or, simpler, perhaps:

ch.bindQueue(ok.queue, ex, id + '=' + status);

Beware escaping issues and so on, of course.

vinnitu commented 7 years ago

oook ) I see Thanks

But it is bad idea! Вecause after disconnect client will send the same count of presence with unbind action..

And what about away->online state? bindQueue on "id;online" ? or unbind "id;away" ?

tonyg commented 7 years ago

Make your queue so it is deleted when the client disconnects. That will remove the bindings pointing to it.

For having "substates", maybe the original idea will work: use two bindings. (Recall that AMQP lets you have multiple different bindings between an exchange and a queue.) The first indicates "online"; perhaps the binding key would be jack=online here. Any subsequent bindings indicate refinements of this -- away, working, idle, whatever. The binding key there would be jack=away etc. The "online" binding would be left alone for the duration of a session. The other bindings would change more quickly.

tonyg commented 7 years ago

BTW the Queue.Declare(auto-delete) parameter might be useful; I see you are setting the exclusive bit, which I think achieves more or less the same thing.