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

Add ability for server to address client #33

Closed alsonkemp closed 13 years ago

alsonkemp commented 13 years ago

Maybe this exists and I couldn't find it, but ...

Also, apologies. This is a feature request without any code to go with it... Lemme know if you'd prefer code...

Whenever I use a Socket.IO-esque framework, I need the server to be able to address the client directly. In NowJS, it looks as though the server can speak directly to the client only in response to a client connection.

Maybe, Google Analytics Async style, the client could pre-declare 'now' with a client_id (I've also added a roomID since it's :

var now = {clientID : 'I-am-a-string-with-the-client-id',
                roomID: 'I-am-a-string-with-the-room-id' };
// NowJS initializes and uses the existing 'now' var.

Now the server can directly communicate with that client:

everyone.client('I-am-a-string-with-the-client-id').directMessage(fromUser, message);
everyone.room('I-am-a-string-with-the-room-id').partyLine(fromUser, message);
ericz commented 13 years ago

Hi alsonkemp,

We're working on a feature with this functionality that will be released soon. Thanks!

ericz commented 13 years ago

Hi @alsonkemp,

I've just built this feature.

https://github.com/Flotype/now/commit/00377ea881c53680281394b6f7736b0ceca206a2

use it as follows:

 nowjs.getClient("some-client-id", function(){
      this.now.whatever():
      // this.user.clientId == "some-client-id" // true
 });
amitda commented 13 years ago

Can someone write a simple example to send a message to a client id?

ericz commented 13 years ago

nowjs.getClient(clientId, function(){

this.now.someFunction();

});

Eric

On Mon, Jun 13, 2011 at 9:07 PM, amitda < reply@reply.github.com>wrote:

Can someone write a simple example to send a message to a client id?

Reply to this email directly or view it on GitHub: https://github.com/Flotype/now/issues/33#issuecomment-1363693

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

amitda commented 13 years ago

Thanks Eric.

Is there a way of integrating this into multi-room chat example?

ericz commented 13 years ago

Yep it can definitely be integrated but the multi-room chat example does not have any of the user interface necessary to integrate it.

amitda commented 13 years ago

I am working on it now... if I am successful, I will upload it here

ericz commented 13 years ago

Great to hear! Let me know if you need further help.

Eric

On Mon, Jun 13, 2011 at 9:23 PM, amitda < reply@reply.github.com>wrote:

I am working on it now... if I am successful, I will upload it here

Reply to this email directly or view it on GitHub: https://github.com/Flotype/now/issues/33#issuecomment-1363755

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

lucj commented 13 years ago

Hello,

Is there an error callback function on now.getClient in case the socket is closed or does not exist ?

Thanks a lot, Luc

jaycrossler commented 13 years ago

Is there a best practice on knowing which clientId to use with getClient? Should we use a userID that already exists? Pull the clientID from the socket.io (and if so, any examples how best to)?

I'm trying to implement a multi-room chat session, where users can join and leave rooms, but still missing exactly how to tell a group that a user should be added or removed from a room.

ericz commented 13 years ago

Hi Jay,

The best way is to use the this.user.clientId which is available whenever this.now and this.user are available.

One way to do what you describe i using the join event

mygroup.on('join', function(){

mygroup.now.notify(this.user.clientId + ' has joined');

// this.user.clientId can also be stored for use in nowjs.getClient later

});

On Wed, Sep 28, 2011 at 4:52 PM, Jay Crossler < reply@reply.github.com>wrote:

Is there a best practice on knowing which clientId to use with getClient? Should we use a userID that already exists? Pull the clientID from the socket.io (and if so, any examples how best to)?

I'm trying to implement a multi-room chat session, where users can join and leave rooms, but still missing exactly how to tell a group that a user should be added or removed from a room.

Reply to this email directly or view it on GitHub: https://github.com/Flotype/now/issues/33#issuecomment-2231849

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

jaycrossler commented 13 years ago

Awesome, that helped alot. I was googling it but not quite understanding until you gave me that hint. I've now got groups working across all my rooms, with the group being joined by the client, but verified that they have membership by the server and then aligned to the group._id from the database. Yay!