Ad

Socket.io Server Object Options, What Are The Implications Between The Two?

- 1 answer

From the socket.io documentation here, you can use the http.Server object, or simply pass a port number into the socket.io server object. What is the difference?

Create the socket.io Object

const io = require('socket.io')({
  path: '/test',
  serveClient: false,
});

Either create a server object from the http library and inject it into the socket.io object

const server = require('http').createServer();

io.attach(server, {
  pingInterval: 10000,
  pingTimeout: 5000,
  cookie: false
});

server.listen(3000);

Or simply pass a port number to the socket.io object and it will make a server for you

io.attach(3000, {
  pingInterval: 10000,
  pingTimeout: 5000,
  cookie: false
});
Ad

Answer

By attaching to a server, you can use socket.io functions on an existing server application, that already serves files and responds to api calls. By passing a port as param, socket.io will spawn a separate server for its functions. If you are using this only for socket functionality, either is fine.

But, attaching to a server is preferable when you want to use SSL. In that case, you can simply pass a HTTPS Server. I could not find any example with port option to use SSL.

Ad
source: stackoverflow.com
Ad