Flotype / now

NowJS makes it easy to build real-time web apps using JavaScript
http://www.nowjs.com
MIT License
1.91k stars 175 forks source link

Feature request: callback for when all everyone.now.methods have executed #49

Closed tommedema closed 13 years ago

tommedema commented 13 years ago

I'm in this situation: I have an incoming stream and receive a chunk. I wish to send this chunk to all now.js clients, but need to pause the stream until all clients have received the chunk.

I can do something like this:

client:

    now.receiveFileChunk = function(id, chunk, cb) {
         //on success
         cb();
    };

and on the server:

 clientsCount = //somehow get ammount of clients here (is this possible?)
 receivedCount = 0;

 stream.pause();
 everyone.now.receiveFileChunk(id, chunk, function() {
      receivedCount++;
      if (receivedCount == clientsCount) stream.resume();
 });

But:

  1. I do not know if it is even possible to get the amount of clients connected
  2. This does not seem like a proper solution

I'm not 100% sure, but it might be a good idea to add a callback to remote function calls for when all clients have received this method. I'm not sure of the implications and overhead involved though...

ericz commented 13 years ago

So right now if you do this, the callback you pass in will be called once for each client, in that client's this.now context.

To get the amount of clients connected you can do what you're doing now. We may add a client count feature so you can easily get the number of clients in a group.

Providing a callback (that runs once) when all callbacks have completed is easy to do, internally it would have to be a counter kind of like what you have now. The difficulty lies in the syntax. Not sure how you would pass this callback in during a remote function call on a group

tommedema commented 13 years ago

@ericz, at the moment I am not doing this because I cannot get the max count. How would I achieve this functionality now?

ericz commented 13 years ago

Oh duhh. My bad you're right you don't have the max. I'll see if its easy to expose a way a count and get back to you

ericz commented 13 years ago

Ok this now exists in the master on github.

Simply do everyone.count or somegroup.count to get the number of users at the time. Make sure you are careful as its possible that the remote call does not return before the usercount changes.

This is an undocumented feature and the api for this will probably change in the future

tommedema commented 13 years ago

@ericz, thanks. Should a new issue be created such as "document group.count and improve API" or do you have an internal ToDo?

Just figured that it should not be forgotten

ericz commented 13 years ago

I have an internal todo already so no issue is necessary, thanks!

On Thu, Apr 14, 2011 at 9:39 AM, tommedema reply@reply.github.com wrote:

@ericz, thanks. Should a new issue be created such as "document group.count and improve API" or do you have an internal ToDo?

Just figured that it should not be forgotten

Reply to this email directly or view it on GitHub: https://github.com/Flotype/now/issues/49#comment_1002232

510-691-3951 EECS Student at UC Berkeley http://ericzhang.com