Socket.io not working with Heroku - node.js

I have looked at all of the Stack Overflow questions regarding the problem. Still, I am unable to run Socket IO along with Heroku. I have looked at https://github.com/socketio/socket.io/issues/1542 and this https://github.com/nodejs/node-v0.x-archive/wiki/Socket.IO-and-Heroku. The sockets run locally.
index.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = process.env.PORT || 8080;
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
var server = http.listen(port, function(){
});
The running sample is here. https://curly-octo-woof.herokuapp.com/

Related

Socket.io different pages for different ports

I have this socket.io:
var express = require('express');
var app = express();
let http = require('http').Server(app);
let io = require('socket.io')(http);
let http2 = require('http').Server(app);
let io2 = require('socket.io')(http2);
io.on('connect', function(socket) {
app.use(express.static(__dirname + '/one_dir'));
});
io2.on('connect', function(socket) {
app.use(express.static(__dirname + '/another_dir'));
});
http.listen(4000, function(){
console.log('check *:4000');
});
http2.listen(5000, function () {
console.log('& check *:5000');
});
I am new in node.js(and in socket too), so I need that on 127.0.0.1:4000 was index.html from /one_dir, and on 127.0.0.1:5000 was index.html from /another_dir,
Can you help me to solve this problem?

How to display logs in node.js?

I run my application is written in Node.js using CMD (Windows OS).
node server.js
There is console.log('Client connected...'); in server file.
But I dont see logs in CMD whene I application is working. How can I fix it?
My code is:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.use(express.static(__dirname + '/bower_components'));
app.get('/', function(req, res,next) {
res.sendFile(__dirname + '/index.html');
});
server.listen(4200);
io.on('connection', function(client) {
console.log('Client connected...');
});

access application port : express

I am trying to build a simple chat application with node.js using Express.My node version is v0.12.7 and express version is 4.13.1.
I need to access the port the application is listening to but I am not getting how to do that even after lot of research over google.
My code in index.js is:
var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
io.on('connection', function(socket){
console.log('a user connected');
});
console.log(app.get('port')); // not working
http.listen(app.get('port'), function(){
console.info('Server listening on port :');
});
module.exports = router;
Please suggest If this can be done and how. Thanks in advance :)
In order to get the port, you're going to have to set it first like this
var app = require('express')();
app.set('port', process.env.PORT || 3000);
...

express, socket.io server for browser client and node.js client

I am trying to set-up an express server with socket.io that will allow node.js clients and browser clients to connect. The browser connects with no problem. The node.js client using socket.io-client give an error:
unhandled socket.io url
Server:
var express = require('express'),
io = require('socket.io');
var app = express();
var host = 'localhost';
var port = process.env.PORT || 8080;
var server = app.listen(port, function() {
console.log('Gulp is starting my app on PORT: ' + port)
});
io = io.listen(server);
app.use('/', express.static(__dirname + '/'));
io.on("connection", function(socket) {
socket.on('clientMessage', function(jsonData, from) {
socket.emit('serverMessage', 'Got a message!');
console.log('Data being sent from', from, 'is:\n' + jsonData);
});
});
Client:
var io = require('socket.io-client')
var socket = io.connect('http://192.168.1.222:8080', {reconnect: true});
socket.emit('clientMessage', 'Hello', 'Pi-Voyager');
The issue was with the way I was requiring my dependencies. It worked on other versions but on my current version it did not.
node -v
v0.12.4
"express": "^4.13.1",
"socket.io": "^1.3.6",
"socket.io-client": "^1.3.6"
With these versions, the following code works:
Server
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 8080;
server.listen(port, function() {
console.log('Gulp is starting my app on PORT: ' + port)
});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.on("connection", function(socket) {
socket.on('clientMessage', function(jsonData, from) {
socket.emit('serverMessage', 'Got a message!');
console.log('Data being sent from', from, 'is:\n' + jsonData);
});
});
Client
var io = require('socket.io-client')
var socket = io.connect('http://10.1.0.47:8080', {reconnect: true});
socket.emit('clientMessage', 'Hello', 'Pi-Voyager');

using socket.io in conjunction with express

I am trying to sent a basic message when users connect and disconnect. Later i want to sent parameters back and forth. But with the following code i do not get the messages print out on the server when they connect/disconnect.
When i replace io.on with server.on i can get the message 'User connected' but not user disconnected when i refresh/close the page. This however is buggy, since not everytime i refresh it logs the message and on first time it sometimes log the message multiple times.
server.js
var //io = require('socket.io'),
http = require('http'),
express = require('express');
var PORT = process.env.PORT || 8080,
HOST = process.env.HOST || 'localhost';
var app = express();
app.use(express.static(__dirname + '/views'));
app.get('/', function (req, res) {
res.sendfile(__dirname + 'index.html');
});
server = http.Server(app);
//io = io.listen(server);
var io = require('socket.io')(server);
io.on('connection', function(socket){
console.log('User connected');
io.on('disconnect', function(socket) {
console.log('User Disconnected');
});
});
server.listen(PORT, HOST, null, function() {
console.log('Server listening on port %d in %s mode', this.address().port, app.settings.env);
});
New code:
var express = require('express'),
app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
var PORT = process.env.PORT || 8080,
HOST = process.env.HOST || 'localhost';
server.listen(PORT);
app.use(express.static(__dirname + '/views')); //need this for my directory structure.
app.get('/', function (req, res) {
res.sendfile(__dirname + 'index.html');
}); //Need this to call my page.
io.on('connection', function( client ){
console.log('Connected');//this does not work.
io.on('disconnect', function( client) {
console.log(client);
});
});
Try reconfiguring your socket, and server instantiation:
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(process.env.PORT || 3000);
io.on('connection', function( client ) {
console.log(client) //this will now work
client.on('disconnect', function( id ) {
console.log('user with ID ' + id + ' has disconnected');
});
});
First of all you need to be sure if you have included the client side socket.io files.
<script src="/socket.io/socket.io.js"></script>
Then you need to initialize the socket connection.
<script>
var socket = io();
</script>
The above changes will be in your index.html file.
Now in server.js file,
io.sockets.on('connection', function(socket) {
console.log("User connected");
socket.on('disconnect', function() {
console.log('Got disconnect!');
});
});

Resources