Sunday 30 December 2018

Port number for Socket.io with Express on Heroku

I use the the following configurations for my node server and react client respectively;index.jsconst express = require('express') const io = require('socket.io')(8000) const app = express() const port = process.env.PORT || 5000 app.listen(port, () => console.log('Listening')) App.jsimport openSocket from 'socket.io-client' const socket = openSocket('http://localhost:8000') This works all good on the localhost. However, when I deploy to Heroku, it does not recognise the port number of 8000 and http://localhost:8000 as expected.Is there configuration to make it assign a port number automatically for the web socket, just like we do with the Express server by using const port = process.env.PORT || 5000 ?​EDIT:This article helped mehttps://devcenter.heroku.com/articles/node-websockets#option-2-socket-ioExample codeAlso don't skip this step:Apps using Socket.io should enable session affinity. If you plan to use node’s Cluster module or to scale your app to multiple dynos, you should also follow Socket.io’s multiple-nodes instructions.heroku features:enable http-session-affinity​

Submitted December 30, 2018 at 11:27PM by eligloys

No comments:

Post a Comment