This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Using memcached as a database buffer for chat messages
A friend of mine was telling me how he used Memcache to build a PHP/Javascript realtime chat room, but I can't figure out how Memcache would help when the data is updated (possibly) every few seconds. Of course, he told me to figure it out on my own.
Can anybody provide any hint as to what advantages Memcache would serve in such an application? (I don't want to know how to do it exactly, just how Memcache would speed things up in an application where the data is constantly being updated.)
Memcache is the PHP interface to memcached (memory object caching)
It really isn't the right tool for the job. He's obviously storing the data as a key - this probably would be faster than hitting a database, but it still sucks.
If you're looking to implement some sort of real-time chat solution I would recommend looking into the following techologies. Read up on them and you'll find a solution which may suit your needs.
Flash (AS3) sockets with a chat server OR
HTML5 Web Sockets OR
COMET (Facebook uses this.)
Flash (AS3) sockets:
There are many ways to build an Actionscript 3 chat system. It's actually pretty easy if you use one of the many existing solutions out there. IE: Smartfox. You can even use the External Interface to trigger Javascript events every time a message is received.
HTML5 Web Sockets
As support for this is pretty flakey I recommend you look into degrading gracefully with Flash. Example of a library you can use: https://github.com/gimite/web-socket-js/
COMET/Long polling
This is quite an interesting approach. Effectively what you're going to be doing is "blocking" the HTTP request server-side by not returning any data until some is available EG:
while(!d = data()) {
// no data...
}
return "{data : d}"
Your browser will in most cases just sit there happily waiting for something to happen. Once data is received the browser will close the connection - this is when you process the data, and then re-open a connection (fire off a new ajax request) so that you are listening for new events!
To accomplish this I recommend using a non-blocking server such as Tornado (http://www.tornadoweb.org/)
Related
A page is sending AJAX call to server and should get item info in response. The array to look-up/return is a rather big one and I can’t hold it in the PHP file to accept the request. So, as far as my knowledge and experience tell, there are 2 methods:
Access database for each request.
Store items in files (e.g. “item12.txt”) and send contents to the user.
My C experience says that opening and closing a file takes much more system time than the rest of the program. How is it in PHP? What is the preferred method (most importantly, resource-wise) – file system or database? Is there any other way you would recommend (e.g. JavaScript directly loading the file with variable array from the server for each request)? Maybe there’s some innovative method lying around you’re aware of?
P.S. On the server-side a number only will be accepted, so no worries regarding someone trying to access files in the server or trying to do some fancy stuff on database.
Sockets
Depending on how many requests you will be handling, you could look into socket connections.
Sockets gives you 2 way communication between the client and the server, which would allow you to do interactive things, as needed.
Socket tutorial 1
Socket tutorial 2
Node.js
node.js is the new kid on the block. You write your own socket webserver, and use javascript to communicate with it. This is a great alternative to Ajax, as it's much more efficient and reliabe.
node.js can be run alongside PHP, and only be used for ajax-like calls.
node.js
node.js socket turotial
There are nothing innovative. If you have low frequency calls to data and you want super simple access to data then use files. But today is much better to use any database (SQL lite) is ok i think. IF you need more performance then use MySQL or NoSQL solutions. Tools made to solve things. Use the right tool for your purpose.
I have been searching the internet for about two days now, trying to understand how web sockets work, I am relatively new at Javascript and PHP, seeing as I started about two or three months ago.
I am extremely confused about what an Http protocol is, and how to perform a 'handshake' with a server as I have been reading about. So unfortunately it is difficult for me to ask a more specific question than the one in the title.
However, even more than I would like an answer to the question in the title, I would really like a resource that will teach me about web sockets in a thorough way, such as a book or an online tutorial.
Any help is appreciated, thanks.
Sockets in general are just TCP streams that you can send data over. HTTP is built on top of them. WebSockets are build on/in parallel with HTTP, and allow for sending data back and forth to a browser in real-time. Standard sockets can do some of this, but browsers have tight security which makes it tough to use standard sockets to communicate with them.
WebSockets are useful when you need a persistent connection to the server from the browser, for real-time applications, or things like push notifications.
I don't know much about PHP implementations unfortunately, but it looks like there is a library here: http://code.google.com/p/phpwebsocket/
Personally, I'd shy away from doing things like this in PHP because PHP/Apache can be pretty heavy, and since the socket is always open while someone is viewing the page, server resources can be used up pretty quickly.
A lot of people like to use NodeJS with socket.io because then you can use JavaScript on the server and the browser, but really it is up to preference. I would look at Socket.IO and find a language that has a good client for it.
It looks like this question might be useful too. Using PHP with Socket.io
Does this one fit the bill?
http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/
If not I can find another.
OK, here's the situation - simple and clear :
On Page A : We perform an ajax request (let's say to intermediate.php) which initiates a background process - let's say back.php
interm.php returns
back.php keeps working until it finishes, while updating its progress in a db.
So, how could Page A get notified that back.php has finished?
(I've thought e.g. of firing an ajax request every 1-2 secs to a script checking the db for back.php's progress, but that seems pretty weird and definitely not efficient...)
Any ideas?
You could do a long-polling or short-polling technique which is quite common and not so weird. It does make a lot of unnecessary requests, but it's the simplest and most supported method out there.
The second method would be using web sockets. Now web sockets through html5 are not generally supported as of now, but an alternative would be to use a library that takes care of fallbacks to other methods such as flash, or even long-polling if your browser / device doesn't support a previous method. A really good library for real-time communication would be Socket.IO. It allows many different implementations of real-time or close to real-time, it also allows you to choose which method of communication you want to use. Some cloud based web services such as Paas don't widely support web sockets. Libraries will go around this by letting you choose or force a specific method of communication: Web Sockets, Flash Sockets, Long-Polling, etc..
But since your using php, you could try this: https://github.com/lemmingzshadow/php-websocket
http://www.htmlgoodies.com/html5/other/create-a-bi-directional-connection-to-a-php-server-using-html5-websockets.html#fbid=8Pw-Juo8eHG
Slightly different approach
http://www.gamedev.net/topic/613642-socketio-php-mysql/
http://devzone.zend.com/209/writing-socket-servers-in-php/
socket.io + php + is this a realistic solution at this point in time
Though as mentioned on some of these sites, using websockets with php is not as straight forward as node.js or twisted python. But nonetheless it's achievable and does work at some degree. I found that having a node.js backend to do the real-time stuff is a lot easier then trying to force a technology that is not meant to be worked in that way.
What you're basically looking for is Comet. Here's a good thread on it:
Using comet with PHP?
I have been in search of making live websites by using PHP. (COMET) I have been searching for a very long time already. (around a month) I have even checked some PHP chat scripts and used on my webserver, but I had some problems on most of them. (will be explained)
So far, most of the people tells the same thing: PHP isn't the best language could be used with COMET. The chat/comet examples are hacky at best.
I am asking this because, I want to have some features on my websites, like allow logged in people to chat with each other. That is where I need an alive connection to PHP pages. I am also planning on making a browser based game, and an alive connection will still be a must!
AJAX was a rare thing 2 years ago, shined with Facebook. Now pretty much everyone uses it, and it became a standard on web development. Now, the COMET based websites are growing. Youtube, Google+, Facebook, EA's Battlelog and such. I believe I should learn how to use it.
Okay, here are my questions. (Some of the information at below are the ones I gathered while searching on Google. Not sure if they're correct or not.)
Some languages like Python have special web servers designed for this job. I believe one of them is called Tornado Web Server. Developed and configured to simulate thousands of alive connections. I believe there is no such option in Appserv, but some people told NGINX can handle it with a decent configuration. Is it true? What configurations should be made? Is there any PHP web servers for this job?
Some of the most suggested things are:
a. Using APE.
b. Using Socket.IO
c. Node.js
Is there any way we can use them with PHP? Which one is the most promising? Could you give some guidance on them? Is there anything else than these?
I have used a comet chat script. Basically, you kept querying database and output the result with flush() to browser by sleeping it with usleep() in a loop. (This one became hard to understand so I will provide a quick code)
while(true)
{
// query database
// output the result
// flush the browser
// sleep for few seconds to lower cpu load
}
usleep() function basically destroyed my web server on Windows based operating systems. Is it normal to rely on usleep() on comet applications which runs on windows based OS? I mean, is there even a way to "sleep" PHP scripts? No matter what I do, CPU load goes to %100 on both WIN and UNIX servers.
Is PHP "really" that weak on this area? Should I give up with PHP and focus on other languages? If so, which language would you suggest? (That language should be promising. For example, there is no much use of AS3 after HTML5 addition, and AS3 is more likely to die soon while JS shines.)
What is WebSync? Can it be used with PHP?
Please bear in mind that I need to use COMET to make following applications:
A chat script, where logged in players will be able to chat eachother.
A browser based game. I already use JSON/AJAX and things like that when coding, but to receive opponents steps, I should pull the data, so an alive connection is needed. (Don't imagine advanced games, I am speaking about games like chess at best.)
I would appreciate if you can give me a short guidance. After all, I have been getting confused day by day since there are SO MANY random articles on internet. People even call setInterval() as COMET, so it is a complete mess.
There needs to be some clarification here. What you're looking for is not language specific per se. If you wanted to achieve the comet functionality with PHP you'd be relying on the Web Server (e.g Apache) to do the HTTP streaming. Also you need to understand that this is not supported in HTTP versions prior to HTTP/1.1. With that said, if you want to have a non-blocking event based web server, you're better off looking at Tornado and Node as suggested.
Comet is a programming technique that enables web servers to send data to the client without having any need for the client to request it This technique will produce more responsive applications than classic AJAX The user must create a request (for example by clicking on a link) or a periodic AJAX request must happen in order to get new data fro the server.
but it's create lots of traffic on your web server. If you want to build chat application in PHP use pusher which is a third party service and easy to use.
here is a link for pusher https://pusher.com/tutorials/realtime_chat_widget
the second suggestion is use ratchet for creating a chat application.
here is link for ratchet http://socketo.me/docs/hello-world
i hope it will help you
How to implement a real fast web chat with PHP?
Has anybody ever wonder why Facebook chat is just so really really fast? even in IE without WebSocket.
Isn't the only way is to setInterval in JS to check for new messages? But I feel it (Facebook chat box) like having instant reaction.
How to implement such great thing with PHP?
The instantaneous chat you're describing is generally acheived by a something called "Long Polling" or, if we're talking about AJAX, "Comet" (Wikipedia talks about it). Polling tends to strain Apache servers, but there are some specialized servers to deal with it like APE. I'm not sure but I think you can do the same with NodeJS and NGINX handles the stress pretty well.
Here's an article here about how to implement a long polling chat with PHP jQuery and AJAX.
Best of luck, and I hope it helped!
I agree with #joseph-szymborski although it would make sense to start looking at WebSocket solutions which fallback to WebSockets via Flash and/or long-polling.
Here are some relevant SO questions:
How to implement facebook like notification on cakephp? - PHP/jQuery
ajax Push system - PHP/Ajax
Apache with Comet Support - the question itself is very good.
PHP Jquery: chat system, what is the Ideal framework for this? - relevant to your question.
If you want to work with PHP or are on shared hosting then I'd recommend looking at a hosted realtime web solution.
You might want to consider Node.js to serve the clients in 'real time' since Long polling with PHP/AJAX may cause strain on your server.
But the implementation itself is an uphill task. Just saying.
Long polling with PHP/AJAX may cause strain on your server.
My typical theoretical implementation of the same:
Create a Node.js server to query a database.
Send variables and/or session data from php to Node js using cURL.
Parse the url in your Node.js server and use the variables to check
for changes in database.
Emit new data if changes occur and send to client.