Vivek Mohan
Vivek Mohan

Reputation: 8356

socket.io not working on heroku

Whenever I try to use require("socket.io"); on heroku it fails with the message "Cannot find module socket.io".

I think it’s an issue with my setup, because the same is running fine in my local node.js server.

What do I have to change?

Upvotes: 1

Views: 3991

Answers (6)

shank_fab_worker
shank_fab_worker

Reputation: 403

These are solutions to socket.io related problems

I hope i will work

  1. Ports in your (index.js or server.js) & (index.html and your client.js) port must be different. (refer below code)

=============your index.js file ======================

(port here is 8000)

const express = require("express")
var app = express();
const http = require('http')
var server = http.createServer(app);
  
const port = process.env.PORT || 8000
server.listen(port,()=>
{
    console.log("Listening at port => "+port)
});
var io = require('socket.io')(server, {
    cors: {
      origin: '*',
    }
});

const cors = require("cors")
app.use(cors()) 

=============your client.js file ======================

port here is 8080

const socket = io.connect('https://localhost:8080/')

=============your index.html file ======================

port here is 8080

 <script defer src="https://localhost:8080/socket.io/socket.io.js"> 
 </script>

Remember your "server.js or index.js" port should be different from "client.js" port (Rememeber this is important)

(index.html and your client.js) port must be same

  1. You should always use 'http' while working with socket.io (refer above code)

  2. U may not included cors as it allows u to have more resourses , without cors heroku prevent some dependencies to not install in heroku (refer above code)

  3. Try replacing "io" to "io.connect"

    const socket = io.connect('https://localhost:8080/')

  4. Must write tag at the end in the HTML

  5. U may forget to add this code which is must in "socket.io"

It is required in your html file

  1. delete "node_modules" and "package-lock.json" and write "npm i" in cmd

  2. This should be in package.json 's scripts

    "start":"node index.js",

I am not talking about nodemon , use simple node over here

  1. May be version is creating a problem , u can avoid it by copying all "devDependencies" to "dependencies" in "package.json" and put "*" in version like this

    "dependencies": {

    "cors": "*",

    "express": "*",

    "nodemon": "*",

    "socket.io": "*"

    },

    "devDependencies": {}

Upvotes: 0

seveves
seveves

Reputation: 1282

websocket transport is working on Heroku in beta state. you can enable it with heroku labs:enable websockets -a YOUR_APP_NAME

Upvotes: 0

Rafa de Castro
Rafa de Castro

Reputation: 2524

Heroku on cedar does not support websockets

Anyway you can use socket.io with

io.set("transports", ["xhr-polling"]); 
io.set("polling duration", 10); 

https://devcenter.heroku.com/articles/using-socket-io-with-node-js-on-heroku

Upvotes: 6

Vivek Mohan
Vivek Mohan

Reputation: 8356

package.json modified as

"dependencies": {
"async":     "0.1.18",
"ejs":       "0.4.3",
"express":   "2.4.6",
"faceplate": "0.0.4",
"socket.io": "latest"   },

And the serverside code is:

var port=process.env.PORT || 3000;
var http=require('http');
var app=http.createServer(function(req,res){
    res.write("server listening to port:"+port);
    res.end();
}).listen(port);
socket=require("socket.io");
io=socket.listen(app);
io.configure(function () { 
  io.set("transports", ["xhr-polling"]); 
  io.set("polling duration", 10); 
});
io.sockets.on("connection",function(socket){
    console.log("new connection");
    socket.on("eventA",function(data){
        io.sockets.emit("eventB",data);
    }); 
});

Working like a charm!!!

Upvotes: 1

michaelcolenso
michaelcolenso

Reputation: 1

You need to change the transport option on socket.IO to xhr-polling with a (10) second duration, according to this project wiki page.

Upvotes: 0

Pavan Kumar Sunkara
Pavan Kumar Sunkara

Reputation: 3025

You have to change your PaaS provider. Heroku doesn't support websockets. Where as nodejitsu is known for support websockets.

Also, you might have forgot adding socket.io in package.json dependency lists.

Upvotes: 1

Related Questions