I would like to know what are the differences between
var socket = io();
and
var socket = io.connect();
using socket.io in my script (client side)
Thank you!
socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).
First of all, every modern browser supports WebSockets these days. Socket.IO uses much more boilerplate code and resources to make it fall back to other technologies. Most of the time, you don't need this level of support. Even in terms of network traffic, Socket.IO is way more expensive.
WebSocket is a protocol, Socket is an endpoint - got it thanks. Also, a simple google search returned "The current API specification allowing web applications to use this protocol(WebSocket) is known as WebSockets" (note the plural).
Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection.
There is no difference.
If you look at the source code for the SocketIO client, io
is declared as follows:
module.exports = exports = lookup;
And io.connect()
is declared in the same way:
exports.connect = lookup;
They both refer to the same (internal) function lookup
.
I think that io.connect
exists to make the client backward compatible with older versions of SocketIO.
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