topright gamedev
topright gamedev

Reputation: 2665

WebSocket PHP server configuration

Does running a WebSocket PHP server requires additional (Apache, PHP, firewall, etc.) configuration? Do we need to allow explicitly requests via ws protocol? If so, how to do it?

I have Linux virtual server with ISPConfig 3. PHP script is running, but client gets no server response.

Upvotes: 2

Views: 3723

Answers (3)

Steven
Steven

Reputation: 2114

First off, websockets is a different protocol entirely from HTTP, so relying on Apache is unnecessary. You might still use Apache so you can point a web browser somewhere, but fundamentally, when your Javascript client code connects to your websocket server, your websocket server should not be running through Apache.

If you really must use PHP for a websocket server, I recommend that you run PHP on the command line and make it run in the background indefinitely. This is important because your code that implements the websocket server should never terminate. Your websocket code will listen on the port number of your choice. Personally, I use ports 9000+, but the choice is up to you.

I think it is vital to note that the language you choose for your websocket implementation can be entirely different from the language you use to process HTTP requests. Personally, I use PHP for HTTP requests, but my websocket server (which provides a real-time video game) is written entirely in C++. The actual client code that connects to the websocket server is written in Javascript.

For a variety of reasons, I think you should choose either C++ or Java for your websocket server, largely because good support libraries exist in those languages, and if desired, you'll have access to strong multi-threading capabilities. If you want recommendations for websocket server implementations in those languages, you can ask me and I'll mention some that work.

Lastly, I want to add that when I decided to write a websocket-based server, I first investigated the possibility of implementing it in PHP, but quickly decided that C++ would work better for my needs. Hopefully you are also able to switch to C++ or Java (or possibly Ruby) as well.

Upvotes: 1

Pitchinnate
Pitchinnate

Reputation: 7556

First thing to check make sure the port (it looks like 8000) is open in the firewall. In ISPConfig by default the firewall only has the following ports open:

TCP: 20,21,22,25,53,80,110,143,443,993,995,3306,8080,8081,10000

UDP: 53

You can see this by logging in and going to System in the ISPConfig control panel. Then on the left hand menu under the System category look for Firewall. It should then show you your firewall rules.

Upvotes: 1

Logan Murphy
Logan Murphy

Reputation: 6230

For this to work for me I had to do the following.

Clone the repository using git.

Edit WebsocketDaemon.php so that line 12 has the following code $this->pid = getmypid();.

Make sure the php executable is in your PATH.

Navigate to websocket\samples\chat\server (which must be moved from the repository to your webserver) in cmd and run the command php index.php start which will cause your cmd to pause.

Start your webserver (any port but 8000 since that is the port your web socket server is running on).

Open two webpages with the following links http://localhost/websocket/samples/chat/client/ in any browser that supports web sockets.

Enter a message and it should work (assuming the webpage said you originally connected).

A word of warning. I tried to use a PHP socket server to make a game but I had memory issues. Here is a post about it PHP Out of Memory Exception.

Upvotes: 1

Related Questions