users join event

46 views
Skip to first unread message

Giuseppe Mastrandrea

unread,
Feb 6, 2012, 1:12:48 PM2/6/12
to APE Project
Hello everyone, proud ape developers!
I'm trying to implement a chat system with custom commands/raws, a bit
different from the example on ape-project website.

There is something, though, that i still can't understand.

I have to update the users list, so i thought i could build a client-
side hashmap containing informations about the currently logged in
users. Fine, but what event should i listen for?

I see, when i try to connect with my user, a row named "LOGIN" in
firebug; when i try to connect from chrome on the same channel i see
instead (on firebug) the JOIN raw, who has got informations similar to
LOGIN raw.

Shouldn't i listen for the "CHANNEL" event to have new
users'notifications?? I read this:

http://www.ape-project.org/wiki/index.php/Protocol_Basics

...and that's what i understood.

So, i ask... what event should i listen for, to track a user's
connection into a channel?

Louis Charette

unread,
Feb 6, 2012, 1:26:37 PM2/6/12
to ape-p...@googlegroups.com
Hi

Have a look at this. When a user join a channel, this event is fired client side.

  - Louis

--
You received this message because you are subscribed to the Google
Groups "APE Project" group.
To post to this group, send email to ape-p...@googlegroups.com
To unsubscribe from this group, send email to
ape-project...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/ape-project?hl=en
---
APE Project (Ajax Push Engine)
Official website : http://www.ape-project.org/
Git Hub : http://github.com/APE-Project/

Giuseppe Mastrandrea

unread,
Feb 7, 2012, 9:01:54 AM2/7/12
to APE Project
Many thanks, i'm using it, already :-)

Another weird problem. I've got two channels (substantially, they're
different pages, with these urls: http://local-domain/chat/1 and
http://local-domain/chat/2)

What i do is trying to connect with a channel named with the URL
number slug ("1", "2", etc),

Now, when i connect to the first room, everything's ok, i can see
users, send PM messages, and so on.
If i try to connect to the other room (http://local-domain/chat/2 )
what i see is the users list from the second room AND the users list
from the first one!

Using firebug console, i can see that into CHANNEL raw i get the users
coming from the OTHER channel... isn't it weird? or maybe i didn't
study the protocol carefully :-)

On 6 Feb, 19:26, Louis Charette <charette.lo...@gmail.com> wrote:
> Hi
>
> Have a look at this. When a user join a channel, this event is fired client side.http://www.ape-project.org/docs/client/events/userjoin.html

Louis Charette

unread,
Feb 7, 2012, 9:24:49 AM2/7/12
to ape-p...@googlegroups.com
If i remember correctly, When a user join a channel, the other client will receive a "userjoin" whatever channel they're in. When the even is triggered, you can check what channel the new user just joined with "pipe.name".

Also, when a user Join a channel, he'll received a "userjoin" even for everybody already in the channel. You can then build the user list with this. You could also code a server side command using the command "userlist" to get a list of everybody in the channel:
http://www.ape-project.org/docs/server/channels/userslist.html

Envoyé de mon iPhone

Giuseppe Mastrandrea

unread,
Feb 7, 2012, 9:31:23 AM2/7/12
to APE Project
Yes, you're absolutely right! I've just noticed it, and as a temporary
solution i've used a simple "if" statement that checks for the
pipe.properties.name. Yes, it's a bit rough, but it works!

Many thanks for your suggests :)

On 7 Feb, 15:24, Louis Charette <charette.lo...@gmail.com> wrote:
> If  i remember correctly, When a user join a channel, the other client will receive a "userjoin" whatever channel they're in. When the even is triggered, you can check what channel the new user just joined with "pipe.name".
>
> Also, when a user Join a channel, he'll received a "userjoin" even for everybody already in the channel. You can then build the user list with this. You could also code a server side command using the command "userlist" to get a list of everybody in the channel:http://www.ape-project.org/docs/server/channels/userslist.html
>
> Envoyé de mon iPhone
>

Giuseppe Mastrandrea

unread,
Feb 7, 2012, 11:20:37 AM2/7/12
to APE Project
Ok, guys, another question:
this time i'm facing the userLeft event. When i close the
browser,after a certain amout of time a LEFT raw is launched, as the
serverreceives no more request from the client. But what if i just
changeroom? I remind you that i'm using a server-based chatroom
system.Here's the scenario:
UserA connects to "http://localhost/chat/1"UserB connects to "http://
localhost/chat/1"
The userlist now is formed by both users.
Now.
UserB connects to "http://localhost/chat/2" (in the same window/tab)
I expect that userlist in chat/1 would refresh, showing just
UserA;unfortunately the LEFT raw is never launched, i suppose that
happensbecause i'm using sessions, so in a certain way there is always
astable connection between client and ape-server, and a userLeft
eventis never fired.
Any idea?
On 7 Feb, 15:31, Giuseppe Mastrandrea <mastrandreagiuse...@gmail.com>
wrote:

Louis Charette

unread,
Feb 7, 2012, 11:35:56 AM2/7/12
to ape-p...@googlegroups.com
I don't use sessions, but in my case as soon as a user refresh the page or close the windows, the "userleft" is always fired after the (annoying) delay.

Envoyé de mon iPhone

ptej...@gmail.com

unread,
Feb 7, 2012, 11:40:45 AM2/7/12
to ape-p...@googlegroups.com
What you could do is create a hook on the JOIN command which would fire userLeft in all other rooms the user is part of but the one joining

Sent via BlackBerry from T-Mobile

Louis Charette

unread,
Feb 7, 2012, 12:38:19 PM2/7/12
to ape-p...@googlegroups.com
Le 2012-02-07 à 11:40, ptej...@gmail.com a écrit :

> What you could do is create a hook on the JOIN command which would fire userLeft in all other rooms the user is part of but the one joining

What if the same user want to be connected to the two chat in two different tabs?

Giuseppe Mastrandrea

unread,
Feb 7, 2012, 12:45:01 PM2/7/12
to APE Project
Yeah, Louis, that's the situation i'm trying to recreate, and the
reason why i just can't hook a userLeft on JOIN command.
Another weird thing: when i've got a user connected on two different
rooms (for example, in different tabs of the browser), and i type a
message in one room, it automatically is shown in the other room, too!
Seems like the following lines:

var chan = Ape.getChannelByName(params.channel);

if (!$defined(chan)) return ["401", "UNKNOWN_CHANNEL"];
chan.pipe.sendRaw(params.raw, params.data);

in inlinepush.js are not doing their job; the 'postmsg' raw, that is
the one launched when a user posts a public message, is sent to both
rooms.

On 7 Feb, 18:38, Louis Charette <charette.lo...@gmail.com> wrote:

Louis Charette

unread,
Feb 7, 2012, 1:11:49 PM2/7/12
to ape-p...@googlegroups.com
I just tested on my chat system and the messages are only sent to the channel it's intended for. I don't have separate room on different page (I use Jquery UI tabs, one for each room the user is connected to). Again, maybe it's because you're using the APE sessions...

You could do the same thing as with the userJoin: When you receive a raw, you check the "pipe.name" property to see if it's intended for the current channel or not.

- Louis

Giuseppe Mastrandrea

unread,
Feb 8, 2012, 5:26:05 AM2/8/12
to APE Project
Honestly, i'm trying to avoid the way you suggest, as i don't want a
client can receive messages from a channel he's not logged in.
And, another weird thing, if i just disable sessions from the client,
everything works as it should. Veeeery strange :-)

Giuseppe Mastrandrea

unread,
Feb 8, 2012, 5:42:45 AM2/8/12
to APE Project
Ok, i think i understood what's happening.
If i use sessions, the client uses the same session in every tab/
window opened. So it is "normal" that a message posted in room 1 is
transmitted and shown in room 2, too. On the other hand, if i disable
sessions, even if a same user is logged in two different rooms, it has
two different pubids; so, while the "public" messaging works as it
should, the personal message system is messed up because of this
"double identity" of the same user.
Trying to manage this situation... i hope i'm able to fix it.

Any suggestion?

On 8 Feb, 11:26, Giuseppe Mastrandrea <mastrandreagiuse...@gmail.com>
wrote:

Sergei Koba

unread,
Feb 8, 2012, 6:48:14 AM2/8/12
to ape-p...@googlegroups.com
Read this please  http://www.xosofox.de/2010/10/ape-user-handling/
If someone hasn't posted this link already. It shows the way to store all pubids for a logged user.

Giuseppe Mastrandrea

unread,
Feb 8, 2012, 6:55:19 AM2/8/12
to APE Project
I read this, and i'm working on that very userlist. Maybe i'll be able
to post something useful in a few hours. Thanks :-)

ptej...@gmail.com

unread,
Feb 8, 2012, 8:27:06 AM2/8/12
to ape-p...@googlegroups.com
I personally have not understand the concept but you could find some answers in the subuser subject. From what I think I understand, once a user is login in one window the other tabs's user become subusers of the first one. Like i said, I'm not sure if my userstanding of the subuser concept is correct I'm just throwing it out there.

http://www.ape-project.org/docs/server/users/subuser.html


Sent via BlackBerry from T-Mobile

-----Original Message-----

Louis Charette

unread,
Feb 8, 2012, 8:55:14 AM2/8/12
to ape-p...@googlegroups.com
I should take a look again at that article, it's been a while!

The way I worked around to send private messages to a user (based on his CMS/forum user id), is to store this ID in the user object server side. When a private message need to be send to him, I call a custom server side command, loop thought all the ape clients, and send to the corresponding client.

Another way to handle private messaging on a chat, based on a CMS userID, is to have this user join a channel named after his ID. You the. Sed everything down this pipe.

- Louis

Envoyé de mon iPhone

Giuseppe Mastrandrea

unread,
Feb 8, 2012, 9:20:49 AM2/8/12
to APE Project
That's an interesting point, Louis!
If i store a triple userID/roomID/pubid server-side i could have a
more flexible way to manage public AND private messaging.
When a user refreshes a page, the server will update the
corrensponding tuple with the new pubid.
Yeah, it's a bit complicated, but MAYBE this way i'll make it :-)

On 8 Feb, 14:55, Louis Charette <charette.lo...@gmail.com> wrote:
> I should take a look again at that article, it's been a while!
>
> The way I worked around to send private messages to a user (based on his CMS/forum user id), is to store this ID in the user object server side. When a private message need to be send to him, I call a custom server side command, loop thought all the ape clients, and send to the corresponding client.
>
> Another way to handle private messaging on a chat, based on a CMS userID, is to have this user join a channel named after his ID. You the. Sed everything down this pipe.
>
>   - Louis
>
> Envoyé de mon iPhone
>

Giuseppe Mastrandrea

unread,
Feb 10, 2012, 5:12:24 AM2/10/12
to APE Project
Ok, after a couple of days full of pain, i think i'm able to post some
useful code. In this snippets i build an hash for each user connected
to ape; this hash substantially implements a "dummy" session system.

This hash is formed by an array of arrays, each formed by the user's
pubid and the room he's logged in. If a user refresh a page, the pubid
is updated in the proper array entry; if a user connects to another
room (i remind you that my system is formed by N rooms that you can
reach from your URL http://localhost/chat/1, http://localhost/chat/2,
... ,http://localhost/chat/N) a couple pubid/room is added to this
array. If a user closes a window, the corrensponding pubid/room array
entry is deleted ("deluser" event is fired). If a user wants to start
a private session, it simply passes the username to the application
server (in my case, that's Django), and then the application server
uses a custom inlinepush to create the session. The ape server, in
this case, checks for the presence of the username in the global
hashlist, and sends a signal to the corrensponding pubids. To achieve
these results, i've customized the xosofox's "nickname.js" to this
one:

var userlist = new $H;

Ape.registerHookCmd("connect", function(params, cmd) {
if (!$defined(params.name)) return 0;
cmd.user.setProperty('name', params.name);
cmd.user.setProperty('room', params.room);
return 1;
});



function listUserNames()
{
Ape.log("--------Current Userlist-------------------------");
userlist.each(function(v,k){
Ape.log(k + ": ");
v.each(function(infoAr, key){
infoAr.each(function(item, index){
//Ape.log(index + ': ' +item);
item.each(function(ob, ind){
Ape.log(ind + ': ' +ob);
});
});

});
});
Ape.log("--------Current Userlist End---------------------");
}

function getPubIdFromName(nickname){
var uis = userlist.get(nickname);
if(!$defined(uis)){
return 0;
}
else return uis;
}

Ape.addEvent("adduser", function(user) {

var name =user.getProperty('name');
var room = user.getProperty('room');
var pubid=user.getProperty('pubid');
var logged = false;
var uis=userlist.get(name);

//if no user instance hash, create one
if (!$defined(uis))
{
var singleInfoAr = new Array(pubid, room);
var infoContainer = new Array(singleInfoAr);
uis= new $H;
uis.set('infoAr', infoContainer);
userlist.set(name, uis);

}else{
infoAr = uis.get('infoAr');
infoAr.each(function(v, k){
if(v[1]==room){
v[0] = pubid;
Ape.log('Updated Pubid!');
logged=true;
}
});
if(!logged){
var singleInfoAr = new Array(pubid, room);
infoAr.extend(Array(singleInfoAr));
uis.set('infoAr', infoAr);
}
userlist.set(name, uis);
}
listUserNames();
});

Ape.addEvent("deluser", function(user) {
var name=user.getProperty('name');
var pubid=user.getProperty('pubid');
Ape.log("Timeout: " + name + ": " + pubid);
var uis=userlist.get(name);
infoAr=uis.get('infoAr');
infoAr.each(function(v, k){
if(v[0]==pubid){
//delete the corrensponding array entry
infoAr.splice(k, 1);
}
});
uis.set('infoAr', infoAr);
if(infoAr.length == 0){
userlist.erase(name);
}
//if last instance left, erase hash
else userlist.set(name, uis);
listUserNames();
});

Please note the "room" parameter in the registerHookCmd function; this
is the room id coming directly from the URL. I pass it to the server
this way:

this.core.start({'name':String(nickname), 'room':room});

Hope this can help someone else, i've worked around it for two days,
and -yeah- i know it's a "raw" solution, but well... it works!

On 8 Feb, 15:20, Giuseppe Mastrandrea <mastrandreagiuse...@gmail.com>
wrote:

ptej...@gmail.com

unread,
Feb 10, 2012, 9:11:40 AM2/10/12
to ape-p...@googlegroups.com
Now that you are working with custom inlinepush command there is little optimization you might want to try out.

Inside the command you should see a line which calls the Ape.config() function. What this function does is fetch a file and get a value out of it. Having this call inside the command means this proccess of fething the file will happen on every inlinepush command call. This could lead to possible memory leaks and extra CPU cycles.

What you could do is call the Ape.config(...) function outside the inlinepush command and store its value in a global variable. Then you could use this global variable inside the inlinepush command instead of Ape.config(...)

Somthing like

var inlinePass = Ape.config(...);
Ape.registerCmd(...,..., function(params, ...){
If(params.password == inlinePass){
...

Is not something that you have to do but you should keep it in mind.

Sent via BlackBerry from T-Mobile

-----Original Message-----
From: Giuseppe Mastrandrea <mastrandr...@gmail.com>
Sender: ape-p...@googlegroups.com
Date: Fri, 10 Feb 2012 02:12:24
To: APE Project<ape-p...@googlegroups.com>
Reply-To: ape-p...@googlegroups.com

Giuseppe Mastrandrea

unread,
Feb 11, 2012, 9:24:30 AM2/11/12
to APE Project
Oh, many thanks, that solution sounds smart! :-)
> reach from your URLhttp://localhost/chat/1,http://localhost/chat/2,
> For more options, visit this group athttp://groups.google.com/group/ape-project?hl=en
Reply all
Reply to author
Forward
0 new messages