Search code examples
phpnode.jsherokusocket.iobuildpack

How to start express server in Heroku multi buildpack php + nodejs


I am creating a webapp on Heroku where I need to some realtime data updates in a part of my php application.

So I am thinking I can use socket.io on express server to provide realtime communication functionality.

How should I run the express when its a php buildpack.

Here is my package.json script section

"scripts": {
    "dev": "cross-env NODE_ENV=development webpack --config config/config.dev.js",
    "build": "cross-env NODE_ENV=production webpack --config config/config.prod.js",
    "start": "node server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

My procfile is...

web: vendor/bin/heroku-php-apache2 public/

and my buildpacks

$ heroku buildpacks
1. heroku/nodejs
2. heroku/php

When I deploy this on heroku, I get no compile error in heroku logs but my webapp gets no realtime updates from socket.io server. In the console log of browser I get a 404 error on http://myapp.herokuapp.com/socket.io/?EIO=3&transport=polling

What am I doing wrong over here?


Solution

  • You would need web: npm start in your procfile if you want to run a node/express web dyno.

    However it sounds like you want a PHP web server, with node/express listening on a different port. You can't really do that on Heroku. Instead, you would need a worker dyno for your node app. You would not be able to directly access your worker dyno from your frontend via a URL, though. Instead, you would need to do something like what is described here.

    See also this answer.