MUHAMMAD SHAHID RAFI C P
MUHAMMAD SHAHID RAFI C P

Reputation: 1229

Socketio not connecting on postman socket connection ui on localhost

Could not connect to localhost:3000
17:02:53
Error: Unexpected server response: 404
Handshake Details
Request URL: http://localhost:3000/socket.io/?EIO=3&transport=websocket
Request Method: GET
Status Code: 404 Not Found

My express server listen on 3000 port and socketio http on 443 port, i am able to connect socket by hosting this on ec2 instance and using ec2 ip with port number on postman socket connection ui, but on localhost connection always failed with above error on postman socket beta ui.

Upvotes: 1

Views: 4603

Answers (2)

Silvan Bregy
Silvan Bregy

Reputation: 2734

You need to call listen only once. If you pass your http/https server to express, app.listen() is enough. (except you want to listen on 2 different ports.. For testing, let's call it only once.)

For accessing it on localhost try setting it explicitly with the parameter hostname in app.listen().

Example:

async function startServer() {
  const app = express();

  const credentials = {key: key, cert: crt};
  const httpsServer = createServer(credentials,app);
  const io = socketIO(httpsServer);
  
  await require('./loaders').default({ expressApp: app, socketIO: io });

  // Let's only call listen once for testing purposes. If you call 
  // listen on the express app, your https server will automatically listen 
  // to the same configuration.
  // httpsServer.listen(4000);

  
 
  const port = 3000
  const hostname = 'localhost'
  // explicitly let the app listen on localhost. If hostname is not 
  // provided, it will take the first found ipv4 interface
  app.listen(port, hostname, err => {
    if (err) {
      Logger.error(err);
      process.exit(1);
      return;
    }
    Logger.info(`
      ################################################
      🛡️  Server listening on https://${hostname}:${port} 🛡️ 
      ################################################
    `);
  });

}

Now you should be able to connect your socket. If it still does not work try to use http module instead of https for better isolating your problem.

Note that your server now only will be accessible using localhost and not over ip. Both may only be possible when running 2 server instances with different hostnames.

Upvotes: 2

MUHAMMAD SHAHID RAFI C P
MUHAMMAD SHAHID RAFI C P

Reputation: 1229

async function startServer() {
  const app = express();

  const credentials = {key: key, cert: crt};
  const httpsServer = createServer(credentials,app);
  const io = socketIO(httpsServer);
  
  await require('./loaders').default({ expressApp: app, socketIO: io });

  httpsServer.listen(443);
  app.listen(config.port, err => {
    if (err) {
      Logger.error(err);
      process.exit(1);
      return;
    }
    Logger.info(`
      ################################################
      🛡️  Server listening on port: ${config.port} 🛡️ 
      ################################################
    `);
  });

}

This is my app.ts file, I am using Ubuntu. so 443 port is not allowed, i changed this port to 4000, now the app listen on the 3000 and socket http server on 4000, but socketio not able to connect with socket url localhost:3000

Upvotes: 0

Related Questions