Asa Carter
Asa Carter

Reputation: 2225

Listening on two ports in the same app in node

I am using socket.io on port 8081 and express with redis on 8080.

I cannot use both on the same port or I get a conflict. When the code below is run the php session is logged in the console and I get the socket.io started message but there is nothing further from socket.io logged to console.

EDIT:

The code below is amended and now I can log the socket.io but I now cannot see the cookie data.

I'm not sure what is wrong?

EDIT - ADDED TEST CODE

Complete code for testing here:

https://www.dropbox.com/sh/nbrmkeq7dwurizi/AADBvl4N5ksnCnFZ6MHFkIyva

Server code:

    var express = require('express'),
    http = require('http'),
    app = express(),
    server = http.createServer(app),
    io = require('socket.io').listen(8081),
    cookieParser = require('cookie-parser'),
    session = require('express-session'),
    RedisStore = require('connect-redis')(session);

app.use(cookieParser());

app.use(session({
    store: new RedisStore({
        prefix: 'session:php:'
    }),
    name: 'PHPSESSID',
    secret: 'node.js rules'
}));

app.use('/', function(req, res) {
    console.log(req.session);
    res.sendfile('/');
});

io.sockets.on('connection', function (socket) {

    socket.on('subscribe', function(room) {
        console.log('joining room', room);
        socket.join(room);
    });
});

app.listen(8080);

Relevant client side code:

$(window).load(function () {

    var socket = io.connect('http://localhost:8081');

    var $conversation_id = $chat.data('conversation-id');

    socket.emit('subscribe', $conversation_id);
});

Calling the socket IO JS file:

<script type="text/javascript" src="http://me*****.dev:8081/socket.io/socket.io.js"></script>

Console Output:

 info  - socket.io started
 debug - client authorized
 info  - handshake authorized w8S6Kc-XSFNq1N_pZErF
 debug - setting request GET /socket.io/1/websocket/w8S6Kc-XSFNq1N_pZErF
 debug - set heartbeat interval for client w8S6Kc-XSFNq1N_pZErF
 debug - client authorized for
 debug - websocket writing 1::
 info  - transport end (undefined)
 debug - set close timeout for client w8S6Kc-XSFNq1N_pZErF
 debug - cleared close timeout for client w8S6Kc-XSFNq1N_pZErF
 debug - cleared heartbeat interval for client w8S6Kc-XSFNq1N_pZErF
 debug - discarding transport
 debug - client authorized
 info  - handshake authorized hkMkIJXGH3ISmbzMZErG
 debug - setting request GET /socket.io/1/websocket/hkMkIJXGH3ISmbzMZErG
 debug - set heartbeat interval for client hkMkIJXGH3ISmbzMZErG
 debug - client authorized for
 debug - websocket writing 1::
 info  - transport end (undefined)
 debug - set close timeout for client hkMkIJXGH3ISmbzMZErG
 debug - cleared close timeout for client hkMkIJXGH3ISmbzMZErG
 debug - cleared heartbeat interval for client hkMkIJXGH3ISmbzMZErG
 debug - discarding transport
 debug - served static content /socket.io.js
 debug - client authorized
 info  - handshake authorized -L7kx-SdPrraKV3DZErH
 debug - setting request GET /socket.io/1/websocket/-L7kx-SdPrraKV3DZErH
 debug - set heartbeat interval for client -L7kx-SdPrraKV3DZErH
 debug - client authorized for
 debug - websocket writing 1::
 joining room 1

Upvotes: 1

Views: 1894

Answers (1)

Nikolay Lukyanchuk
Nikolay Lukyanchuk

Reputation: 874

You don't send response to client in your route /. Create valid html page before using socket.io

app.get('/', function(req, res) {
   res.end('<html><body><script type="text/javascript"></script></body></html>');
});

And send valid response to client with your Relevant client side code

Upvotes: 1

Related Questions