views:

53

answers:

3

I'm developing a chat module for my application...

1) I'm opening a window for users to chat, is there way that when users close the chat window, I can update status of that record...i mean event for the closed browser ?

2) I know that default session time is 24mins, so after 24mins of inactivity, user will be kicked out from the site and will asked to loggin once again.

How to delete/flush the data in my database, when user has no activity for 24mins(when user is logged out from the session due to inactivity)

Thanks for the kind help.

+1  A: 

1) You'll need javascript onUnload event for this one. It'll send an asynchronous query to your webserver, setting the offline status of the user. However, you should not rely solely on this event and also set up the 24 mins auto-offline timeout because it is not guaranteed that the user is using javascript.

2) I think your best option here is running a cron job (every 30 mins or so?) that queries your database, identifies the users whose last activity was more than 24 mins ago and then deletes the associated data.

Revolt
is there any affect on my server, if i run cron job for every 30mins...i mean i heard that server will be slow, at the time of cron jobs, is this true
luvboy
And how can i know from the database users table, whose activiy is more than 24mins...Should i store the inactivity time or wht ?
luvboy
Well, running a cronjob every 30 mins is certainly better than checking it on every page load. Depending on the size of your DB this 30 mins parsing could slow down the server yes. Do you really need to delete the data every ~24mins? If not you could setup a cronjob every day at 2AM for example, to minimize the slowness
Revolt
You should store the timestamp of when the user made the last action. Then on the cleanup script you make a simple time subtraction between the saved time and the actual time. If the difference is greater than 24 mins you clean it up
Revolt
+1  A: 

Store every chat entry's timestamp in UNIX_TIMESTAMP. When a new chat entry incoming check every entries timestamp where timestamp is smaller than now - 24 mins. Kick users.

fabrik
+1  A: 

1) use the unload event

2) if you are developing a chat, i guess that you have a periodical function that calls the server constantly to retrieve the messages. Each time this function is called, the inactivity time will be reset, even if the user haven't sent a message. If you want to logout the user when he doesn't write anything in 24min you cant rely on the php sessions.

What you can do is: save in the db, the last time the user wrote a message on the chat, and each time you use your periodical function, validate if the user hasn't wrote anything in the last 24 mins

pleasedontbelong