3

Is there a way of checking the number of currently subscribed clients to a certain publish function? Problem is that I have different groups where every group has its own unique ghash. When a user chooses to leave a group and enters a new one, this ghash changes and THE SAME publish function is subscribed, although with a different ghash of course.

So I am looking for a way to check how many clients are subscribed to each group/ghash at a time (at the server side). I've been fiddeling around all day with stuff like this but it does not work that well to be honest. I am also listening for the "unsub" event of sockets and all that but still ... this is all buggy as hell.

If some one's interested in my whole code, you can find it here! (I found it too long to paste it here into my post.)

I really hope someone can help! :-)

cheers, P

EDIT: Or in other words: Is there a way to count the number of clients currently connected to a sockjs websocket where all these websockets were called with the same params?

=========================================================================

EDIT 2: New version: LINK

For some reason this is not working at all ... No inserts are made because the ghash provided to the subscription is NEVER equal to any of the actual socket subscriptions (--> see lin 20: ghash is never equal to ghash2). I just don't understand how this is possible? the whole subscription function is called each time the Session ghash changes. How can this var never be equal to the param submitted to the actual socket (submission)? (it's always also a ghash, but always a ghash of another group).

I am really lost here! :-(

Community
  • 1
  • 1
Patrick DaVader
  • 2,133
  • 4
  • 24
  • 35

3 Answers3

1

I now see you are doing straight old node style socket.io programming. I've done similar things in node projects. This is maybe the real question. On the docs for Meteor they don't even use the word socket. Maybe someone else would get into that new question with you, but this question about tracking subscribers is answered by this answer.

I think meteor is a new world, and will handle such stuff for you, if you adapt to its way of thinking. For example, make a collection of messages, with a field for chatroom. Each client picks their chatroom, finds those messages.find({chatroom:'box5'}), and displays them. A new message automatically goes to every client that is listening to that chatroom. Let Meteor use sockets for you.

Answer to counting clients subscribed to something:

Pseudo code: Make an object to hold the counts of each subscription signature counts = {}

on signup, Make a string that represents the subscription uniquely, add it to your counting object. counts['params as string'] += 1;

on signout counts['params as string'] -= 1;

The logic to know when no one is still subscribed is this: done = (0 == counts['params as string'] )

Jim Mack
  • 1,437
  • 11
  • 16
  • I am sorry but could you explain a little bit more what you mean by this cryptic answer?! ;) Without any text, real code n stuff it is really very hard to get what you mean by this answer! – Patrick DaVader Sep 11 '13 at 17:28
  • maybe you could use pastebins "create a new version of this paste" function and put in your code there?! – Patrick DaVader Sep 11 '13 at 17:34
  • Is this better? Instead of thinking you can count them, you have to keep track and the count is provided within the tracking. – Jim Mack Sep 11 '13 at 20:12
  • posted a new version! please have a look in my upper post! I used a Collection again instead of an object because I need to access it elsewhere but in essence I think my new solution is like what you suggested, right?! – Patrick DaVader Sep 11 '13 at 21:52
  • Yes, what you did is like my suggestion, but I have another comment. I modified my answer above. – Jim Mack Sep 11 '13 at 23:14
0

Apparently as I know as of now it is not possible to do this. I did some research and tried many things but for some reason sometimes multiple websockets are opened for trasnfering the same data to the same client. --> counting the number of clients connected is impossible via this approach. Just triggering an event when my ghash changes is also not good enough as a close of the browser window would not trigger it.

I think having a functionality to count the number of clients "viewing the same data changes" (can't think of a better way to put it) would be awesome. Maybe some meteor core dev can just put his/her 2 cents in here so we know if this is even possible at all.

I hope someone can come up with a solution at some point .. I can't! :(

Patrick DaVader
  • 2,133
  • 4
  • 24
  • 35
  • here's a way to do it : http://stackoverflow.com/questions/30800136/how-to-count-the-current-number-of-subscriptions-to-a-meteor-publication/30814101#30814101 – looshi Jun 13 '15 at 19:50
0

My user-status package tracks the number of clients connected to a Meteor app by tracking the number of subscriptions to a global publish function. You may be able to draw some inspiration from it. It's not granular at the per-publication level, but you can certainly do the same thing for publications that you are interested in.

https://github.com/mizzao/meteor-user-status

The main points to note are

  • each open session will call the subscription (users may have more than one tab open)
  • each time a user logs in our out, the subscription will update
  • you can read the per-session id in the publish function
  • you can listen to the close event for the SockJS socket for browser tabs being closed, etc.

I don't think it would be too hard to do this for groups; I am doing the same thing for another project.

Andrew Mao
  • 35,740
  • 23
  • 143
  • 224