centrifugal / jscent

Node.js client to interact with Centrifugo v1 HTTP API
MIT License
11 stars 5 forks source link

Connect/disconnect events #7

Closed nanom1t closed 5 years ago

nanom1t commented 7 years ago

Hi,

Is it possible to track connect/disconnect (subscribe/unsubscribe) events on server side? For example, user connected to websocket/subscribed to channed and the 'connect' event will be emitted on server side. The same thing with disconnect.

Socket.io has this feature:

...
io.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

I understand that Centrifugo runs in separate process with Go, but maybe it is possible with API callbacks and so on. I need this feature to track when user is online in my application. It will be awesome to have this feature.

Thanks

FZambia commented 7 years ago

@nanom1t hello, no - this is not possible at moment in a way you want as Centrifugo runs as separate service and though introducing some sort of webhooks is theoretically possible - I don't think it's a good idea - when you need such a tight integration with your service it's better to use sth integrated in code.

What is your concrete use case for this btw?

nanom1t commented 7 years ago

@FZambia Thanks for your response.

I'm working on Node.js application, which is similar to small social network (not very large for now) and I'm already using Centrifugo in my project.

Each user have own profile in my app. I just want to show when user is online or show last login time on his profile. Now user subscribes to his personal channel in Centrifugo and receives messages from server during all session time.

So I thought that it will be awesome if I can listen to connect (disconnect) events to channel and change user status at that time (for example, from offline to online).

I'm not sure that it is the best way to do that, but I think it will work for me.

Thanks

FZambia commented 7 years ago

Showing when user is online looks like a presence request to his personal channel. You can also update last login time sending AJAX request to your app manually when user opens a page - without Centrifugo at all. And periodically update that last active status - also without Centrifugo.

I don't see a clean way how having connect/disconnect events can help you a lot with online status. There can be races (fast reconnect) that must be handled somehow. Also there can be massive reconnects that can result in self ddos. And many other problems I suppose.