Variable Syncing Issue

58 views
Skip to first unread message

swalkergibson

unread,
Jan 2, 2012, 12:31:22 AM1/2/12
to nowjs
Hey everyone,

I am wondering if anybody has any wisdom for me on the following
issue. I am attempting to get variable syncing to work, but am having
difficulty doing so. The following is code that worked for me using
node 0.4.3 and Now 0.6.1. I recently updated to Node 0.6.6 and Now
0.7.6.

CLIENT
=========
now.receiveMessage = function(name, message, color){
$("#messages").append("<br><span style='color:" + color + ";'>" +
name + ": " + message + "</span>");
$("#leftside").scrollTop(1000000);
if(this.now.banned){
window.location = "/message/banned";
}else if(this.now.isPrivate){
location.hash = this.now.privateChatRoom;
}
}

SERVER
=========
everyone.now.changeRoom = function(newRoom){
this.now.isPrivate = false;
nowjs.getGroup(this.now.room).now.receiveMessage("SERVER",
this.now.name + " left the room.", "red");
nowjs.getGroup(this.now.room).removeUser(this.user.clientId);
nowjs.getGroup(newRoom).addUser(this.user.clientId);
this.now.room = newRoom;
userList[this.user.clientId]['room'] = this.now.room;
everyone.now.updateList(userList);
this.now.receiveMessage("SERVER", "You're now in " + this.now.room,
"red");
}

everyone.now.privateChatRequest = function(userId, userName){
userList[this.user.clientId]['room'] = userId;
userList[userId]['room'] = userId;
nowjs.getGroup(this.now.room).removeUser(this.user.clientId);
nowjs.getGroup(userId).addUser(this.user.clientId);
this.now.room = userId;
nowjs.getClient(userId, function(){
nowjs.getGroup(this.now.room).removeUser(this.user.clientId);
nowjs.getGroup(userId).addUser(this.user.clientId);
this.now.isPrivate = true;
this.now.privateChatRoom = userId;
this.now.room = userId;
this.now.receiveMessage("SERVER", "Your request for a private chat
has been granted. You are now speaking with an Admin.", "red");
});
everyone.now.updateList(userList);
this.now.receiveMessage("SERVER", "You're now in a private chat with
" + userName, "red");
}

I am particularly interested in the this.now.isPrivate variable. I
need to set it in order to properly update the client's URL hash. I
implemented the following code on the client, which did not work.

now.receiveMessage = function(name, message, color){
$("#messages").append("<br><span style='color:" + color + ";'>" +
name + ": " + message + "</span>");
$("#leftside").scrollTop(1000000);
if(this.now.banned){
window.location = "/message/banned";
}
now.ready(function(){
isItPrivate = this.now.isPrivate;
});
setTimeout(function(){
if(isItPrivate){
location.hash = this.now.privateChatRoom;
}
}, 1000);
}

Is there anything special on the server that needs to be done to set
that variable in order to reference it on the client side? I am at a
loss for the moment...

swalkergibson

unread,
Jan 2, 2012, 1:09:02 AM1/2/12
to nowjs
It occurs to me that I should add more context to this post.
Basically, I am creating a multi-room chat application. I am currently
working on the ability for an administrator to initiate a private chat
with a specific user. The way that I am implementing this is
dynamically creating a new NowJS group based on that user's clientId
and adding both the administrator and that specific user to that
group. This newly created dynamic group is referenced in the URL by
its hash parameter. Initiating the private chat from the
administrator's connection works fine, both clients get dropped into
this new, private room. However, when the other user clicks away from
the private room, the URL hash changes to the desired room (the
expected result), but then reverts the URL hash parameter back to the
private room hash.

Eric Zhang

unread,
Jan 2, 2012, 4:38:14 PM1/2/12
to no...@googlegroups.com
Hi Shane,

On your client side code, you should simply use `now.` instead of `this.now.` 

`this.now` is used only on the server.


Eric
--
Co-Founder @ Flotype, makers of NowJS

Eric Zhang

unread,
Jan 2, 2012, 4:41:03 PM1/2/12
to no...@googlegroups.com
Hmm actually this.now should still work on the client side as long as this is not bound to a different object than `window`.

So nevermind on previous message.

What kind of error messages are you seeing?

Eric

swalkergibson

unread,
Jan 2, 2012, 5:09:33 PM1/2/12
to nowjs
The weird thing is that there are no error messages associated with
it. Basically, what happens is the following. After the admin brings
the other user into the private room, the URL hash is updated to
reflect the other user's client ID (which, in the eyes of now, is a
new group). If the other user clicks on a room tab to go to another
chat room, the URL hash is updated to the other room, but then flips
back (almost immediately, but not so fast that I cannot see it happen
in my browser's URL bar) to the private room URL hash. When this block
of code is inside the setTimeout, the URL hash changes after the
duration of that setTimeout function.

Essentially, the point of all of this is to automatically bring the
other user into the private room when the administrator wants them
there. Then, when they are done with the private chat, the user should
be able to enter another room by clicking on its tab (not browser tab,
but a tab in the chat room UI). As a result, when an administrator
initiates the private chat, the this.now.isPrivate flag is set to true
on the other user's this.now object. Then, when that other user clicks
on the UI tab to switch to another room, the everyone.now.changeRoom
function is executed, which should set the this.now.isPrivate flag to
false, which it in fact does on the server-side. For whatever reason,
it is not shared on the client-side.

On Jan 2, 2:41 pm, Eric Zhang <e...@nowjs.com> wrote:
> Hmm actually this.now should still work on the client side as long as this
> is not bound to a different object than `window`.
>
> So nevermind on previous message.
>
> What kind of error messages are you seeing?
>
> Eric
>
>
>
>
>
>
>
>
>
> On Mon, Jan 2, 2012 at 1:38 PM, Eric Zhang <e...@nowjs.com> wrote:
> > Hi Shane,
>
> > On your client side code, you should simply use `now.` instead of
> > `this.now.`
>
> > `this.now` is used only on the server.
>
> > Eric
>

swalkergibson

unread,
Jan 2, 2012, 11:58:07 PM1/2/12
to nowjs
I implemented a work-around to the problem I was having, essentially,
I just added a context parameter to the receiveMessage function so
that specific user's URL hash would be updated. However, I think it is
cleaner to utilize the variable syncing functions of Now. Given that
the code I was using before the upgrade to the newer versions of Node
and Now, I am led to believe that there may be something going on
there. However, that is confounded by the fact that I am far from the
world's best programmer, and am an admitted Javascript n00b. The other
thing that is really weird is that it seems like it is only this
variable that is not being shared - there are a couple of others in
there that seem to be working fine.

Just thought I would let you guys know!

Steve Wang

unread,
Jan 3, 2012, 2:46:43 PM1/3/12
to nowjs
Eric:
Looks like now.ready(func) calls func in the context of the now
object, so you were probably right. Although it probably should've
thrown an error in the JS console.
Reply all
Reply to author
Forward
0 new messages