Can I run a websockets server using PHP on Heroku?


I'd like to use websockets with PHP on Heroku in the same app.


It's not currently possible to have a websockets server running alongside a PHP app on Heroku. Only web dynos can receive HTTP traffic on Heroku, and only on the specified port. Other languages (e.g. Ruby, Node) handle traffic without an external web server (such as Apache or Nginx used with PHP), and can thus not only detect and handle WebSocket upgrade requests, but also serve both HTTP and WebSockets from the same code base:

PHP applications require a web server (e.g. Apache or Nginx which are provided by the PHP buildpack) to handle the traffic. By the time PHP is invoked, the request has already been processed, and WebSockets handling is no longer possible. It's also not possible to handle WebSockets traffic using a regular SAPI.

The solution for PHP users is to run two apps, one for regular HTTP, and one for WebSockets. Alternatively you can use an add-on provider such as Pusher instead.