Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is native PHP support for Web Sockets available?

People also ask

Are WebSockets still used?

Websockets are largely obsolete because nowadays, if you create a HTTP/2 fetch request, any existing keepalive connection to that server is used, so the overhead that pre-HTTP/2 XHR connections needed is lost and with it the advantage of Websockets.

What is WebSocket support?

The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.


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:

  • Apache Module: https://github.com/disconnect/apache-websocket
  • Ratchet: https://github.com/cboden/Ratchet
  • Wrench: https://github.com/varspool/Wrench
  • PHP WebSocket: http://code.google.com/p/phpwebsocket/

Note: IE10 is now released in Windows 8

Also see: Ajax push system


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: How to create websockets server in PHP.

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.