I have made this little chatting application using php and mysql,
and i wondered if a user could be logged off as soon as they closed the window. I tried using timestamps, but I can't get that to work. If someone has a little code for that, I wouldn't mind getting it :)
Codeigniters Session class as something called sess_expire_on_close so it is possible. You can take a look at this class here: https://github.com/EllisLab/CodeIgniter/blob/develop/system/libraries/Session.php
As suggested in the comments - comet is the right technique to use. It's basically an endless stream of AJAX calls, where you'll need to heavily depend on short timeouts.
If you have a chat application, it's best to treat it as a service, I think. Then your service can be responsible for session management. Then the client side can make periodic calls to communicate that it's still there. If the service doesn't receive the "still here" token in n units of time, then the service can update the user's status to logged out.
Related
Currently I'm making a chat application where only admin and users chat, no user-to-user chat . The design is: every chat is stored in database and each 2 seconds user and admin make an AJAX request (to a php file) to see if there is a new chat dialogue, and if there is, pull the data into the textbox. It all seems normal and working good.
Problem is as more user is talking to admin at the same time the AJAX request is becoming a lot, and by testing, the web performance already decreased with only 5 users chatting at the same time. And the input is slow too, every time user press enter they got to enter the data into database first before the admin can read it (and vice versa).
I have been told that using JSON is a recommended way, but I have no idea how to do it, can someone please at least tell me how's the design or flow is going to be if use JSON? Or is there a better way to make it? (by the way, using node.js is currently impossible for my current hosting, so don't put it in suggestion lists, sucks I know).
You should change the AJAX responder phps output to JSON. (you can use the json_encode php function for example.) And you should parse(eval) this in javascript.
I am a bit sceptic. It think It could reduce the network usage by more than 50%.
Maybe you can try a message queue, like 0mq or rabbitmq.
There are a lot of chat examples around.
I have made a dating website where I have use one to one chatting application like facebook. When one user send any message to another user it showing into their popup chat box, but I have done this using ajax. Which I have run in every interval using javascript setInterval function. But I think the process is not optimize one. I don't want to make unnecessary request to the server each time, rather it only trigger when there is some new message for that user. Is there any other way to do it or any other protocol which using by big site like facebook, gmail?
You could do this using WebSockets, but that requires both a server implementation and a web browser that supports it.
Another technique is to use Long Polling, but again, this requires work on both the client and the server. The advantage is that this is a cross browser compatible technique.
I agree with Josh that WebSockets would be worth looking into, however if you don't have access to the server you could use something like Firebase for the back end.
https://www.firebase.com/index.html
Read into Long Polling. It's what facebook uses. Basically your client makes one Ajax call and nothing gets returned until there is data to push to it. I'm pretty sure it requires some custom server configuration so if you're developing on shared hosting it isn't going to cut it. Long Polling would be the right, albeit, more complicated way of doing this if efficiency is what you want.
Server-Sent Events seems to be another option.
A chat example: http://motyar.blogspot.com.es/2012/01/simple-chat-application-with-html5.html
Documentation: https://developer.mozilla.org/en-US/docs/Server-sent_events
Im starting a new project very soon, and im considering long polling to notify the users that they have a new private message/Notification that they have to check, really similar to how Facebook uses to notify you that someone has posted something about you/Liked a photo of you..
From what I have read, cometd seemed like a really good option to start with.
Then, other ways started emerging, like: Socket.io, and node.js ..
Now, my question is; Which one do you think is the best option for this case and why?
What I need to do exactly is the following;
User 1 logs into their account
User 2 sends User 1 a message which gets stored into the database and a flag is generated, (If possible?!)
The PHP script responsible for User 1 should pick up the flag, and push a notification to User 1
I know how to take care of the javascript side, but I have never done anything similar to long polling.
Im using jQuery as javascript library and PHP for the server side.
So recommendations and any good resources to do this?
It would be beneficial to use a combination of php and also node.js. node.js is made for use with persistant connections, and push communication instead of poll.
http://nodejs.org/
Here is a quick video i found: http://vimeo.com/29099827
I am wondering, if there is a real-time way time ping a user and see if that user is still only.
My site uses jQuery, PHP and CRON jobs
so I was wondering what would be the best way to check that the user is still online.
Each user is issues a cookie aka session key but I don't want to just go by that.
I know real-time analytics are able to it, using javascript so I wonder if I could also do the same thing.
Thanks
it could be done via continues ajax call from client but not recommended.
I suggest use flash or something like that to reduce number of ajax request to the server.
Any method you choose will work the same as what you mentioned, it will just update more often.
You can use an ajax call that pings the server every x seconds and as soon as that stops you'll know they left at most x seconds ago.
A similar option is to use WebSockets or Flash to hold a socket connection with the server and mark them as logged off the moment that socket connection is broken.
Both these options are a bit more difficult to scale than sessions and cookies.
Question is very confused and ambiguous.
No you can't 'ping' a user. You can ping an IP adderss - but the IP addresses you see at your server probably don't map on a one-to-one basis with users.
Which user? All users? Why? What will you do with the information? Have you got a better reason than "I don't want to" for not using session data?
"I know real-time analytics are able to" - then you must know the method they use to do this - or are you just buying the sales speak?
I guess only way to get "real-time" data is to use web-sockets.
another way which is less acurate ( with delay ) is to use repited ajax calls. say 1 per 60 seconds.
I'm here to ask you if what I think is the right way to go around coding this.
I have a site that receives private messages and I wish a flag to show up the moment the person receives a message. Should I check for new messages every 3 seconds and show the flag if there is a new message or is there a better way?
If I did it in ajax, I was thinking check every 3-5 seconds for new messages, and once there's a flag, stop checking for more.
My only concern is, if it checks every 3-5 second, will it cause any lag or glitchyness for the person when they're typing? Lets say they're typing out a paragraph somewhere, I don't want their writing to glitch while it checks those 3-5 second intervals.
One of my coder friends mentioned there is a method with Ping(?) or something like that. Where the person is always connected to the server and when there's a change it notifies the user. I'm totally unsure of how this works.
Anyone know how facebook does it? haha.
Thank you!
If you have done the AJAX well, it should not lag/glitch while typing. Something like 3-5s is good as its fast enough but won't slow down server/browser.
Did he mean "push"? In push the messages are pushed to client in realtime, client is not asking if there is new messages. This is most likely the method Facebook is using.
One of my coder friends mentioned there is a method with Ping(?) or
something like that
To be honest I really don't like periodic refresh(polling at intervals), because tt has scaling problems(I got notice from hosting provider when using periodic refresh). You should use more efficient transports like for example:
WebSocket
Adobe® Flash® Socket
AJAX long polling
AJAX multipart streaming
Forever Iframe
JSONP Polling(cross domain)
To use this you could for example use:
hosted solution pusher with API and generous free plan. This gives you max 20 concurrent and 100,000 messages per day, but no SSL, so do not transmit sensitive information over the wire. They also provide third-party PHP client available at github implementing REST API.
socket.io (I like this a lot)
tornado
netty
Anyone know how facebook does it? haha.
For chat they use Erlang. They also have open-sourced tornado(see link above) which they required from friendfeed which they acquired in the past. Facebook is a PHP-shop, but they decided to not use PHP for this, because PHP can not yet do this efficiently. Anyway they are using one of the efficients transports above.