Socket.io + NodeJS doesn't work on Heroku

前端 未结 1 1440
栀梦
栀梦 2020-12-15 10:04

I am doing an API and it\'s on Heroku. But I am having some problems with the socket.io only on the heroku side, when I test it in local everything goes fine. The API is com

相关标签:
1条回答
  • 2020-12-15 10:49

    So there's a couple things here. If you'd like to use Heroku's Websocket service (which is pretty great actually), you're going to need to rework your code to use the einaros/ws implementation of websockets--and then add the service via heroku command line. https://github.com/einaros/ws/blob/master/doc/ws.md

    however, since you've already coded your app to socket.io, I would simply rework how you're instantiating the socket.io library:

    var express = require('express'),
        app = express(),
        server = require('http').createServer(app),
        io = require('socket.io').listen(server),
    
    server.listen(process.env.PORT || 3000);
    

    This should solve your issue, but let me know what the logs show. I think the hang up is that your app, and your socket are running on two different ports.

    After you've created your sever, you can listen for socket events with:

    io.sockets.on('connection', function(socket) { //'connection' or any other event
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题