Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodejs Socket.io Express failed to handshake after calling HTTP API #3118

Closed
1 task done
almgwary opened this issue Nov 19, 2017 · 2 comments
Closed
1 task done

Nodejs Socket.io Express failed to handshake after calling HTTP API #3118

almgwary opened this issue Nov 19, 2017 · 2 comments

Comments

@almgwary
Copy link

almgwary commented Nov 19, 2017

You want to:

  • report a bug

Current behaviour

I am using SocketIo with Nodejs, Express server and MongoDB, I followed the documentation . it works fine when connecting multiple clients they can send messages to each other without any problem . when I made an Http request, I cannot connect any new clients and get this error.

socket.io.js:7370 WebSocket connection to
'ws://localhost:28232/socket.io/?userId=userAmr&EIO=3&transport=websocket&sid=wNTTgrUD-PSeNaIcAAAF'
failed: Error during WebSocket handshake: Unexpected response code:
400

the other connected users before the Http request can continue sending messages without any problem.

I debugged the Socket library and found the client socket request go to connect function then fire errorCode:1

This this my code

/**
 * Create Express server.
 */
const app = express();


// API endpoint
app.get('/api/test',(req,res)=>{
    res.status(200).send({test:"test"});
});



/**
 * Init socket
 */
const server = require('http').Server(app);
const io = require('socket.io')(server);


io.on('connection', (socket) => {

        // emit message to group
        socket.on('emitMessage', (data, callback) => {
                io.emit('emitMessage', data);
        });
});

This my question on stack overflow https://stackoverflow.com/q/47375814/6786941

@almgwary
Copy link
Author

i found the problem, the package express-status-monitor was making this wrong behavior .

try to remove it, and it will work perfectly

 // comment these lines, as they making the issue
 // const expressStatusMonitor = require('express-status-monitor'); 
 // app.use(expressStatusMonitor());

The final code:

let app = require('express')();

// these two lines were making the problem, please comment them. if you want to reproduce the problem enable them again 
// const expressStatusMonitor = require('express-status-monitor');
// app.use(expressStatusMonitor());

let http = require('http').Server(app);
let io = require('socket.io')(http);
let port = process.env.PORT || 3000;

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
});



app.get('/api/v0/availabilities',(req,res)=>{
    res.status(200).send({test:"test"});
});


io.on('connection', (socket) => {

    // emit message to group
    socket.on('emitMessage', (data) => {
        io.emit('emitMessage', data);
    });
});

http.listen(port, function(){
    console.log('listening on *:' + port);
});

This my question on stack overflow https://stackoverflow.com/q/47375814/6786941

This my answer on stack overflow https://stackoverflow.com/a/47411701/6786941

@kamalptw
Copy link

kamalptw commented Dec 7, 2018

@almgwary You, Sir, Are too good. I was facing the exact same issue. I removed expressStatusMonitor and it worked like a charm! Thank you! After a lot of days of searching, I was able to finally solve this after using your suggestion. Thanks a ton!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants