Is native PHP support for Web Sockets available?

PhpJavascriptWebsocket

Php Problem Overview


Looking for Hello World Type Example of Web Sockets Implementation:

Here is Socket Create reference from php.net but this looks more low level than Web Sockets.

I want to use this Web Sockets as shown here on caniuse.com which is now implemented in all new major browsers.

A Google search turned up this Nets.TutsPlus site in which I can use the JavaScript example code...but I need to know how to implement the server-side in PHP not Java, Ruby, or Node.js as in the example.

Is PHP Socket Create relevant? Does PHP natively support Web Sockets? I guess just a point in the right direction for PHP implementation would help.

Actually the tutorial has a broken link to phpwebsockets...is this the library one should use?

Websockets.org has a test application, but no mention of PHP.

Php Solutions


Solution 1 - Php

There isn't native support in terms of there being a standard PHP WebSocket object natively available.

You'll need to use a library.

The next thing to consider is how the WebSocket server runs. Normally PHP runs in Apache, Nginx (via FastCGI) or on Microsoft IIS (via Fast CGI). With Apache and IIS this may be a problem as it's not really built with persistent connections such as WebSockets in mind. I'm not sure about Nginx. This is why most PHP WebSocket libraries will be built as standalone libraries to be run as their own processes.

See:

Note: IE10 is now released in Windows 8

Also see: https://stackoverflow.com/questions/7594425/ajax-push-system/7596103#7596103

Solution 2 - Php

Yes, it's possible to do PHP + Websocket very simply, without any third party library (like Ratchet, often mentioned).

This article is a great lightweight example. (I lost hours with complex solutions, all of them including a few libraries, until I found this useful, simple, article)

You can find more detailed instructions about this here: https://stackoverflow.com/questions/14512182/how-to-create-websockets-server-in-php/65974136#65974136.

It uses a constantly-running PHP server, that you start from command-line with php websockets.php, with an event-loop (similar to the Node.JS way). It's 100% possible to use native PHP functions like socket_create, etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser656925View Question on Stackoverflow
Solution 1 - PhpleggetterView Answer on Stackoverflow
Solution 2 - PhpBasjView Answer on Stackoverflow