I use NodeJS both as the server and the client. (No web browsers) Server seems to be working, but client does not connect. I tried to set the port in the options but it did not work. I try to connect to the port 3000 over telnet and it connects to something, so the server is listening. What am I missing here?

Server:

var express = require('express');
var app = express();
var path = require('path');
var server = require('http').createServer(app);
var io = require('socket.io')();
var port = 3000;

server.listen(port,"127.0.0.1", () => {
    console.log('Server listening at port %d', port);
});

Client:

const io = require('socket.io-client');

const socket = io("http://127.0.0.1:3000",{reconnect:false});

socket.on('connect', function () {
    console.log('connected to the server');
});

1 Answers

0
eol On Best Solutions

As mentioned on the offical socket-io github page, you need to pass the http-server instance to the socket-io server:

...
var server = require('http').createServer(app);
var io = require('socket.io')(server);
...