Project

General

Profile

Callback on session open/close

Robert Larsen
Added over 4 years ago

Hi

I need to keep track of all live full jids. Is it possible to know when a new session is opened and when it is closed?

Or will I have to hack the SessionManager class?

Thanks


Replies (8)

Avatar?id=6023&size=32x32

Added by Artur Hefczyc TigaseTeam over 4 years ago

There are a few ways to do it, depending on your use case. If you want/can extend Tigase with your custom code, I suggest to write a plugin which can be triggered by user login event (probably resource bind packet) and by user logout. Then this plugin can do whatever it wants with the information.

Another approach would be to use the presence forwarding feature. You can forward users presence to any XMPP address you want. You can have kind of a bot under the address which would collect all this information. The problem with this is that it would forward all presence states changes (busy, away, etc...) not just available and unavailable. Also presence unavailable does not necessarily mean that a user session was closed from the specification point of view but that would be a rare situation. But this way you do not need to load any custom code into the Tigase.

Added by Robert Larsen over 4 years ago

I can extend Tigase and yes, the resource bind packet is probably the right one for session start, but too often the user never logs out but simply times out (especially since tablets are becoming dominant) so session close cannot be seen that way.

Relying on presence would require the users to actually indicate presence, but I would like to know about all sessions and not only the ones that are "available".

So, I will add the necessary code to SessionManager (wherever objects are added or removed from the sessionsByNodeId map if I am not mistaken).

Maybe this could come up again? Maybe having a SessionListener would be a good idea?

Avatar?id=6023&size=32x32

Added by Artur Hefczyc TigaseTeam over 4 years ago

Robert Larsen wrote:

I can extend Tigase and yes, the resource bind packet is probably the right one for session start, but too often the user never logs out but simply times out (especially since tablets are becoming dominant) so session close cannot be seen that way.

Of course Tigase handles this correctly and simple disconnection by a client is signaled correctly as session close and presence unavailable is sent to all buddies in the roster.

Relying on presence would require the users to actually indicate presence, but I would like to know about all sessions and not only the ones that are "available".

So, I will add the necessary code to SessionManager (wherever objects are added or removed from the sessionsByNodeId map if I am not mistaken).

Modifying SessionManager is not a good idea as you will have to maintain your custom code every time you update your system to more recent version of our code. As suggested before, the best way to do this would be a custom plugin. The plugin can intercept resource bind packet and also can be triggered on user's session close/disconnection.

So it would be the easiest way to intercept all users' logins/logouts.

May I ask, what are you going to do with this information in your plugin?

Added by Robert Larsen over 4 years ago

Modifying SessionManager is not a good idea as you will have to maintain your custom code every time you update your system to more recent version of our code.

Yes, I located where to insert code (six or seven places). In time we will probably change the system to be more xmpp-like and use presence, however the system we have now don't always indicate presence that way. We will exchange ejabberd for Tigase first and then (if possible and if it makes sense) modify the system.

As suggested before, the best way to do this would be a custom plugin. The plugin can intercept resource bind packet and also can be triggered on user's session close/disconnection.

Finding the bind packet is easy, but knowing when the connection is cut is more difficult and would require having timers for each connection. Tigase does this perfectly but I see no way of having it tell my plugin when it happens (unless perhaps if we started using presence more rigorously).

May I ask, what are you going to do with this information in your plugin?

We have a large gaming web site and use xmpp for IM, chat and as a middleware for multiplayer games. The page you are currently at is indicated through the resource of the jid, så we can indicate that user Bob is looking at user Bettys guestbook and also plays Backgammon in another tab...all through xmpp.

We would like to track this even if Bob is unavailable, which he is if we see no actions from him for...some minutes (forgot how many), so the server might see a presence unavailable stanza which does not mean he has cut his connection.

So a callback when Tigase actually thinks he has gone away would be nice (and this is what I added...I think :-) )

Avatar?id=6023&size=32x32

Added by Artur Hefczyc TigaseTeam over 4 years ago

Robert Larsen wrote:

Modifying SessionManager is not a good idea as you will have to maintain your custom code every time you update your system to more recent version of our code.

Yes, I located where to insert code (six or seven places). In time we will probably change the system to be more xmpp-like and use presence, however the system we have now don't always indicate presence that way. We will exchange ejabberd for Tigase first and then (if possible and if it makes sense) modify the system.

You just need a single plugin, take any file from tigase.xmpp.impl as an example code. Do not modify SM.

As suggested before, the best way to do this would be a custom plugin. The plugin can intercept resource bind packet and also can be triggered on user's session close/disconnection.

Finding the bind packet is easy, but knowing when the connection is cut is more difficult and would require having timers for each connection. Tigase does this perfectly but I see no way of having it tell my plugin when it happens (unless perhaps if we started using presence more rigorously).

You do not need presence for that. Tigase will signal to plugin of user's logout/disconnection even if no presence is exchanged. All your plugin needs to do is to implement XMPPStopListenerIfc interface.

May I ask, what are you going to do with this information in your plugin?

We have a large gaming web site and use xmpp for IM, chat and as a middleware for multiplayer games. The page you are currently at is indicated through the resource of the jid, så we can indicate that user Bob is looking at user Bettys guestbook and also plays Backgammon in another tab...all through xmpp.

This is pretty interesting application. However, this is kind of inefficient way to do it as you have to re-authenticate the user each time it changes a page which is slow and throws lots of load at your server. It is not a problem when you have 1,000 users but it will be a problem when you have 100,000 or more users. A better way would be to send a presence packet each time the user changes the page. The presence packet can contain additional payload with information about the page and other user's activities.

We would like to track this even if Bob is unavailable, which he is if we see no actions from him for...some minutes (forgot how many), so the server might see a presence unavailable stanza which does not mean he has cut his connection.

I think you mistaken presence unavailable with presence away or something like that. Please note, the server does not generate presence unavailable if the user did not disconnect, unless the user sends it intentionally. So if the Bob does not do anything for several minutes but is still connected, there is no presence unavailable. And your client software can send different presence stanzas every now and then.

So a callback when Tigase actually thinks he has gone away would be nice (and this is what I added...I think :-) )

This should/could be done easily on the client side. The client software can detect user's inactivity and do something about it, like sending presence away or something like that.

Added by Robert Larsen over 4 years ago

All your plugin needs to do is to implement XMPPStopListenerIfc interface.

Nice...I missed that one.

I have tried creating a plugin that listens for stanzas, but it never receives them. I tried both as a processer and preprocesser, and many different return values from supElementNamePaths/supNamespaces. I can modify it to receive incoming (even all ) but not the outgoing that contains the jid that the server generated for him. Must be messing something up. Could you have a quick look at it?

Avatar?id=6023&size=32x32

Added by Artur Hefczyc TigaseTeam over 4 years ago

You cannot intercept outgoing resource bind packet in this particular case. But intercepting incoming resource bind should be good enough.

Avatar?id=6023&size=32x32

Added by Artur Hefczyc TigaseTeam over 4 years ago

Actually you can intercept outgoing stanzas in the XMPPPacketFilterIfc plugin but I do not recommend doing it unless this is really necessary. I would suggest to stick to putting your logic in the packet intercepting incoming resource bind packet.

    (1-8/8)