CodeIgniter Forums

Full Version: Track users in an chat system
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Fabdrol[/eluser]
Hi folks!

I'm building a little AJAX chat application (eventually not so little, but for now). The chat part isn't that hard, but I've got a little trouble to develop a way for the app to 'know' wether the user on the other side is still connected, or not.

(its one-on-one chat)

imagine you are chatting with your aunt, and your aunt's dog is on fire. So she shuts down her old Commodor 64 'internet-ready edition' to get a gun to stop the dog from suffering. But the moment she disconnects, you want to know you can stop telling her about the disastrous date you had last night, since she aint listening anymore.

It would be easy if a user hits a 'disconnect' button of some kind, but thats not the case, the user could also click on 'back' in the browser, just enter another adress in the adres bar or even close the browser. In all that cases, the system should know that the user has left the chatroom, but when a user refreshes the page the chat room should stay there...

Do you get what I mean? I haven't written any code yet, since I have no idea wat to write. That's why I post here first, I think that the collective minds of all you geniuses know much more than I.

Thanks in advance!
Cheers
Fabian

El Forum

[eluser]JanDoToDo[/eluser]
Everytime a user changes page on your site or does something, you could update a db table with the user_id and a timestamp. In the chat if the current time - timestamp is e.g. > 5 minutes you know the user is idle or not there.. Just an idea Smile

El Forum

[eluser]WanWizard[/eluser]
Looks like you're trying to add some state to a stateless mechanism... Smile

Assuming that both chat clients have our server and application between them, you could implement a timeout ("If I haven't seen an AJAX request within x seconds I assume the connection is lost").

This might be an issue if you want to allow for pauses in the conversation ("I've got to go do a number two, back in 30 minutes..."), in which case you will have to implement some sort of keep-alive feature, like having your AJAX script call http://mysite.com/keepalive/userid every 5 seconds or so...

El Forum

[eluser]Fabdrol[/eluser]
I thought about that too, but I don't think that's the right solution since users can go away for a couple of minuts, leaving the chatroom open. Instead, I was thinking of implementing some sort of javascript thingy (listening to the unload event, for instance) problem is, that unload is also fired when you reload the page. And we don't want the chat session to be destroyed when a page is reloaded!

-- Fabian

El Forum

[eluser]WanWizard[/eluser]
I'm afraid it's going to be a keep-alive system.

As I mentioned, the Web is a stateless environment, it would be handy to have an event triggered on 'browser-close', but AFIAK that doesn't exist.

I you look closely at other chat systems, like Jabber or MSN, you'll see they send regular updates to the server as well to maintain a logged-on state.

El Forum

[eluser]Fabdrol[/eluser]
True, and true. But I was hoping to find some hacked-up version of the session lib which allows sessions to be destroyed when the browsers is closed, but not when the page is refreshed.
When a user goes to another page on my app, I can add logic there to destroy the chat session. Then there's one more way: unload in case of the user going to another site (eg, http://www.google.nl) and in that case i'd destroy the session using jquery/ajax + a controller method.

Think that's possible? (still stateless, but close enough)

El Forum

[eluser]BrianDHall[/eluser]
You can try to catch onunload or any other such 'leaving' event, but be careful of other pages on your site logging out - many users like to browse other pages while in chat, so I know I'd be really annoyed if I couldn't use another website because if I do it logs me out of chat - it would certainly be incentive not to use either your chat or your website Wink

Indeed though, a KeepAlive system along with a simple timed ping would be able all you can do. Ever X seconds send a "I'm still here!" to the server. Short enough that you know pretty quick to stop talking about how ugly your date's dress was, but not so you are slamming your server and slowing down your users internet connections due to your state-obsession.

It's how every other system works, TCP/IP, FTP, you name it. Lack of communication = disconnection in the internet world, so you just measure disconnection as a result of looking at the time of last communication. QED

El Forum

[eluser]Fabdrol[/eluser]
Hi Brian,

Thanks for the reply. I was just experimenting with the sort of thing, when you posted it.
Think I'll end up with something like that in the end anyway. Last night I wrote the chat system,
it is completely scalable and completely secure right now, the one thing left is styling and add some code to ping the users.

I think I can even accomplish that by extending the current 'update' code. Right now, a javascript method checks the server every one seconds
for new messages, and of course that doesn't run when not on the page. If I add code to make it check whether the other's state has or has not been updated in the last x seconds, and add code to update the users' own state, I believe it'll do.

Thanks a lot folks!
When my code is out of private beta I might release a Lib for it, depending on how much people are interested.
So, anybody interested in a codeigniter AJAX Chat library?

Cheers,
Fabdrol

El Forum

[eluser]Cro_Crx[/eluser]
I started creating a chat system in PHP/MySQL a while ago using AJAX to make it work in real time. There's two different ways you can make the application be able to check which users are still active.

The first is to use comet. Comet allows your Web server to hold long HTTP states to appear to be stateful. This works really well although you do need server side software to make this work correctly. http://en.wikipedia.org/wiki/Comet_(programming)

The second method is to use 'alternative' methods to achive longer http sessions. You can do things by having the users send a message to the server every xxx ms (Initially when creating my application users would update their status every 2500ms). This is the easiest to implement althogh it's the least efficent method. My application would comsume a few MB's of data every hour if the user was simply iding.

Another way of doing the alternative method is that users send a request every minute and the server sits in a loop and perfoms the required actions (checks for new messages and updates the user status etc....). This works better than the first method as it reduces the amount of requests the client has to send (you send one every minute at least though to make sure the client hasen't timed out).

The last way of doing the alternative method is to use a hidden iframe and keep a long http session which updates the chat in realtime. This method is good as the client only has to make one initial request although it's complex to implement.

With my application I ended up going with the second alternative method. Using a long http sessions. You can find heaps of good info on this website http://ajaxpatterns.org/. Have a look at the Http Streaming and periodic refresh pages. They have examples of chat programs in the articles with various methods they use to appear stateful.

El Forum

[eluser]developer10[/eluser]
[quote author="Fabdrol" date="1262448532"]Hi Brian,

Thanks for the reply. I was just experimenting with the sort of thing, when you posted it.
Think I'll end up with something like that in the end anyway. Last night I wrote the chat system,
it is completely scalable and completely secure right now, the one thing left is styling and add some code to ping the users.

I think I can even accomplish that by extending the current 'update' code. Right now, a javascript method checks the server every one seconds
for new messages, and of course that doesn't run when not on the page. If I add code to make it check whether the other's state has or has not been updated in the last x seconds, and add code to update the users' own state, I believe it'll do.

Thanks a lot folks!
When my code is out of private beta I might release a Lib for it, depending on how much people are interested.
So, anybody interested in a codeigniter AJAX Chat library?

Cheers,
Fabdrol[/quote]

I am interested in having such a library, i plan to have a chat application in my future app, but i'm unable to build it myself (at least for now)

I'd be very grateful if i get that from you as soon as it is completed (assuming that it's going public then Smile )
Pages: 1 2