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?
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
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.
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!!!
You need to change the transport option on socket.IO to xhr-polling with a (10) second duration, according to this project wiki page.
websocket transport is working on Heroku in beta state. you can enable it with heroku labs:enable websockets -a YOUR_APP_NAME