Akhilesh B Chandran
Akhilesh B Chandran

Reputation: 6608

HTML5 - WebSocket in shared hosting

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. :(

Upvotes: 8

Views: 15748

Answers (2)

behman14
behman14

Reputation: 41

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

Upvotes: 4

drew010
drew010

Reputation: 69937

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.

Upvotes: 5

Related Questions