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.

Ask on Stack Overflow

Engage with a community of passionate experts to get the answers you need

Ask on Stack Overflow

Heroku Support

Create a support ticket and our support experts will get back to you

Contact Heroku Support