2

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.

6
  • What PHP code are you using? When I did it I did not need extra config. Commented May 5, 2014 at 18:34
  • Chat example from github.com/morozovsk/websocket Commented May 5, 2014 at 18:36
  • Do you get a message when you run the command php index.php start? Or does your command line pause? Commented May 5, 2014 at 18:42
  • It outputs: PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php5/20090626/mysqlnd.so' - /usr/lib/php5/20090626/mysqlnd.so: cannot open shared object file: No such file or directory in Unknown on line 0. And pauses. Commented May 5, 2014 at 18:55
  • 1
    Thank you for helpful note, Charles. Commented May 5, 2014 at 20:00

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I've done all steps, except "start webserver" (Apache is already running). After a long wait, client closes connection with 1006 code ("Abnormal Closure").
Thank you for a warning about out-of-memory exception. Notice taken. :)
1

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.

Comments

1
+50

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.