admin管理员组

文章数量:1191748

Looking at the following example from the Socket.IO lib (or any other example):

// note, io(<port>) will create a http server for you
var io = require('socket.io')(80);

io.on('connection', function (socket) {
  io.emit('this', { will: 'be received by everyone'});

  socket.on('private message', function (from, msg) {
    console.log('I received a private message by ', from, ' saying ', msg);
  });

  socket.on('disconnect', function () {
    io.emit('user disconnected');
  });
});

It seems that Socket.IO is always dependent on a http server, to the point that it will create one for you, like in the example above.

Since websockets are not HTTP, why are http servers needed? If it is only for fallbacks, why is it so thoughly integrated?

Looking at the following example from the Socket.IO lib (or any other example):

// note, io(<port>) will create a http server for you
var io = require('socket.io')(80);

io.on('connection', function (socket) {
  io.emit('this', { will: 'be received by everyone'});

  socket.on('private message', function (from, msg) {
    console.log('I received a private message by ', from, ' saying ', msg);
  });

  socket.on('disconnect', function () {
    io.emit('user disconnected');
  });
});

It seems that Socket.IO is always dependent on a http server, to the point that it will create one for you, like in the example above.

Since websockets are not HTTP, why are http servers needed? If it is only for fallbacks, why is it so thoughly integrated?

Share Improve this question asked Jun 15, 2016 at 12:56 omertsomerts 8,8382 gold badges33 silver badges40 bronze badges 8
  • Websocket is created when you make upgrade from http to websocket, so it kind of does need http. – Azamantes Commented Jun 15, 2016 at 13:03
  • @Azamantes . Please explain the same in detail. Even I want to know more about it. It'd be great to see the answer to this. Thanks – Akshay Khandelwal Commented Jun 15, 2016 at 13:05
  • 1 socket.io isn't a pure Websocket server/implementation, it depends on HTTP for its initial connection setup. – robertklep Commented Jun 15, 2016 at 13:10
  • 1 @omerts "Once a connection to the server has been established (including a connection via a proxy or over a TLS-encrypted tunnel), the client MUST send an opening handshake to the server. The handshake consists of an HTTP Upgrade request, along with a list of required and optional header fields.", see tools.ietf.org/html/rfc6455#section-1.4 page 17 – Azamantes Commented Jun 15, 2016 at 14:33
  • 1 The ws package is for you. It creates a plain WebSocket server, without a fully-functional HTTP server. – Константин Ван Commented Nov 24, 2022 at 14:30
 |  Show 3 more comments

1 Answer 1

Reset to default 29

Since websockets are not HTTP, why are http servers needed?

The premise on which your question seems to be based is that socket.io is a websocket library, which it isn't.

It's a real time, bidirectional event-based communication library (blurb from homepage). One of the transports that it uses are websockets, but it also provides other transports (XHR/JSONP), not just as a fallback but also for situations where websockets aren't supported/required/wanted.

On top of the transport, it offers additional functionality like segmentation (namespaces, rooms), acknowledgements, broadcasts, etc.

Even when websockets can be used, the initial connection setup it done over HTTP. Also, a socket.io server will attach to an HTTP server so it can serve its own client code through /socket.io/socket.io.js.

That said, although you don't need an HTTP server to regular websockets, there's no denying that the websocket protocol was designed with HTTP in mind (as to allow HTTP and websocket servers to co-exist on the same TCP port).

本文标签: javascriptsocketiowhy does it need an http serverStack Overflow