8

I used to have a small chat app(which was almost working), that uses PHP, jQuery and MySQL. The volume of users is very small (only my friends uses it). I used long polling method for this.

And now, I am thinking about using HTML5 Websockets for this, because it is a lot more efficient. And also most of my friends are using Google Chrome(which already supports HTML5). I have gone through some tutorials that talks about HTML5 websockets. And I have downloaded the phpWebSocket from github. I have gone through the code. But the readme file says that the PHP page that listens to incoming connections should be run using "PHP -q" from commandline. So, I have searched what this "q" flag would do. And I found that it runs the page in quiet mode. So, when I run this in quiet mode what is happened ? It would run endlessly ? Will this running process affect the system resources ?

This PHP page should run the entire time. Then only the connections could be accepted. Isn't it ?

I am having a shared hosting package with HostGator. And they allow cron jobs too. And my present chat app(that uses long polling method) inserts all the messages to database. When the user polls, it would search for any new messages from the database and then output them (if any).

So, I am bit stuck here. :(

2 Answers 2

5

It should be run from the command line because as you suspected, it is intended to run endlessly. It binds to a socket on the server and listens for incoming connections. It can't be reliably run from the browser.

The "-q" option tells it not to output any browser headers such as X-Powered-By: PHP or Content-Type: text/html

It will consume as much memory as PHP requires as long as its running. Your memory footprint on startup with no clients will vary between configurations. The more connected clients, the more cpu, memory and socket descriptors you will use. It uses select so it is efficient socket handling.

Also, since you're on shared hosting, you probably won't be able to use it because your user will most likely not have the ability to bind to a port and listen for connections.

As you can see in the demo, the URL to connect the WebSocket to is ws://localhost:12345/websocket/server.php. Unless you have a webserver capable of using WebSockets, you will have to run something like phpWebSocket that acts as a server and listens on a port other than 80.

Hope that helps.

2
  • One more question: how do we use SESSIONS along with this WebSockets ? Or is it not possible in this case ? I mean, I want to store particular data about the user. Or say some verification signature. Is it possible to use SESSIONS for this ? Or create separate datamembers in the User class : github.com/esromneb/phpwebsocket/blob/master/… ? Jan 25, 2012 at 10:49
  • As far as I know, you wouldn't be able to use sessions with the websockets in this scenario. Since the browser is just connecting to a socket on the server, there are no cookies or session functionality. You may be able to send some sort of login sequence once the websocket connects (perhaps the session id from the session cookie) and you could then fetch some information about the user. Until websockets are more integrated into Apache/PHP I think you will have to come up with your own solution. See stackoverflow.com/questions/5353362/…
    – drew010
    Jan 25, 2012 at 17:52
4

The shared hosting package for HostGator does not allow clients to bind to local ports for incoming. This might be part of the problem.

http://support.hostgator.com/articles/pre-sales-policies/socket-connections

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.