83

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.

Kev
  • 118,037
  • 53
  • 300
  • 385
  • 2
    This can probably help: [PHP Socket Programming, done the Right Way](http://christophh.net/2012/07/24/php-socket-programming/) – ChocoDeveloper Aug 30 '12 at 18:46
  • 2
    Googling "PHP Websockets" gives you many results such as: http://socketo.me/, http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/, http://code.google.com/p/phpwebsocket/ – scrappedcola Aug 30 '12 at 18:52
  • 1
    I don't see a clear direction on how to approach Web Sockets in PHP....is it natively supported or do I need another library...would be my first question on how to implement?.... –  Aug 30 '12 at 18:55
  • 1
    In your searching, don't confuse sockets with web sockets. – Brad Aug 30 '12 at 18:59
  • 2
    Excellent question. This is what I want to ask. – Gunah Gaar Apr 08 '13 at 06:35
  • ["Difference between socket and websocket?"](https://stackoverflow.com/q/4973622/8112776) – ashleedawg Nov 29 '20 at 13:14
  • No, but you can create a PHP client by following this tutorial: https://www.piesocket.com/blog/php-websocket/ – Anand Singh Sep 27 '21 at 09:37

2 Answers2

53

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: Ajax push system

Community
  • 1
  • 1
leggetter
  • 15,248
  • 1
  • 55
  • 61
  • 6
    Do host providers support this? small ones...like godaddy.com, wiredtree.com, do I essentially just pick a library and then drop it into my web folder...thing is I'm on a LAMP stack... –  Aug 31 '12 at 14:00
  • The general answer to this is that hosting providers don't like you creating persistent connections - and definitely not potentially 100s or 1000s of persistent connections. Personally I don't think self-hosted WebSocket usage on shared hosting is going to be an option for quite a while. It certainly doesn't integrate particularly nicely with the standard LAMP stack IMHO. For now, I think a hosted service such as [Pusher](http://pusher.com) (who I work for) is the best solution for developers on a shared hosting LAMP stack. – leggetter Aug 31 '12 at 14:48
  • 2
    I checked with Technical Support at Wired Tree and they said it was fine. –  Sep 01 '12 at 21:33
  • 1
    @leggetter can you provide a link on persistent connection hate? my bandwidth usage plummeted when I switched to 100% ajax. It fell at about the same rate after going 100% websocket. So I'm very curious. Thanks in advance! –  Nov 02 '13 at 16:36
  • @Gracchus "persistent connection hate"? If you can please clarify I'll do what I can to help. – leggetter Nov 03 '13 at 23:23
  • @leggetter was just trying to conserve space. re: "The general answer to this is that hosting providers don't like you creating persistent connections - and definitely not potentially 100s or 1000s of persistent connections." the only thing that occurred to me was that it's assumed websockets would use more bandwidth, but i use much less bandwidth with websockets vs ajax vs postback. was wondering if you had details on that comment. thanks again in advance! –  Nov 03 '13 at 23:40
  • @Gracchus the problem isn't the bandwidth, it's the number of persistent connections. In general, it's the cost of maintaining the connections that really eats up resource usage. If you take this into consideration when you're using shared hosting then clearly having one application with lots of persistent connections can impact other applications on the shared host. PaaS providers such as Heroku and OpenShift have only relatively recently adding WebSocket support - Heroku are still in beta. OpenShift wrote about some of the challenges they faced https://www.openshift.com/blogs/paas-websockets – leggetter Nov 04 '13 at 12:21
  • @leggetter can you give a link about the resource usage of persistent connections? i didn't see it in the link you provided, but thank you for showing me the pain languages like php, ruby, etc have to go through to get up and running with apache! thank you very much again in advance! –  Nov 04 '13 at 14:06
  • @Gracchus No link to a write-up that answers your question directly I'm afraid. You can piece together the bits of the puzzle by reading up on Apache MPM modules [this is a good start](http://www.vps.net/blog/2013/04/08/apache-mpms-prefork-worker-and-event/) (see Prefork in particular), [Apache MPM event module](http://httpd.apache.org/docs/current/mod/event.html), [older resources](http://wiki.dreamhost.com/Web_Server_Performance_Comparison) and [newer ones](http://www.theorganicagency.com/apache-vs-nginx-performance-comparison/). Sorry, they don't directly answer the question. – leggetter Nov 04 '13 at 18:39
  • 5
    @Gracchus - What leggetter is saying is pretty much common knowledge. Opening sockets to the outside world comes with many problems, especially when the service is often engineered by novice coders. You have process managing issues, connection management issues, security issues (each open port is basically a door into the system), etc. In addition, if the service isn't broken, isn't riddled with security issues, and runs efficiently, it basically becomes a competing service for the provider. Not many business models are open to such a thing. – JSON Jan 17 '14 at 07:57
  • For which versions of PHP this is true? Is this still correct for the latest ones? – YakovL Sep 12 '16 at 15:49
  • A really good [article/tutorial](http://www.phpbuilder.com/articles/application-architecture/optimization/creating-real-time-applications-with-php-and-websockets.html) which demonstrates on how to use PHP websockets with 'ghedipunk/PHP-WebSockets', Elephant.io, Rachet and Wrench – Ikhlak S. Oct 11 '17 at 14:13
  • I also made Sandstone: https://github.com/eole-io/sandstone and I use it when I need a websocket server with a rest api and push events – Alcalyn Jan 30 '18 at 10:11
12

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.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • 1
    This is exactly what I want !. THANK YOU DEAR. I'm really interested in how things work in low-level programming. – aariow Apr 18 '21 at 18:46
  • It is a nice project. You can't start it or call the socket if you are using an unsecure http page. So this example just works on unsecure connections (not on https) – Adriano Aug 06 '21 at 10:33