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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With