Socket.io-client: What's the difference between io.Manager and io.Socket?

Created on 22 Jun 2018  ·  14Comments  ·  Source: socketio/socket.io-client

I am kind of new to the socket.io-client. After reading the doc for several times, I still don't understand what the differences between io.Manager and io.Socket. Basically I can generate a new socket from io(). but how can I generate a manager instance? Meanwhile, what's the differences between Manager's event and Socket's event.

thank you.

question

Most helpful comment

Yeah it seems like the documentation tells us how to create these, but doesn't tell us what makes them unique and when to use one or the other.

All 14 comments

+1

+1

Yeah it seems like the documentation tells us how to create these, but doesn't tell us what makes them unique and when to use one or the other.

as I try so far, most of manager property could be directly applied in io piece
e.g.
socket = io( 'http://192.168.2.2:8088' + Param.chatNamespace + '?parammmm=1', { query: { key: 'Michael' }, path: Param.path, transports: ['websocket'], autoConnect: false, // like this, could be found in manager piece } );
And that is what I want perfectly.

metoo

I'd love to know the difference, if there's one, between:

  • io(url, options)
  • io.connect(url, options)

And what's the pourpose of:

  • new Manager(url, options)

I can't seem to find anything usable in the documentation apart a very detailed but cold description of the parameters.

Also, if I can use in my browser's client html page either socket.io or socket.io-client

Thank you and sorry, I see lots of work has been put on this library but it's very confusing to get an hang of it.

I completely agree. I can't find any difference between the manager and socket instances.

+1,

+1

Menh!! so no response on this yet??? like wth!!

Hi! I've added more details about the Manager and the Socket classes here and here.

The Manager manages the Engine.IO client instance, which is the low-level engine that establishes the connection to the server (by using transports like WebSocket or HTTP long-polling).
The Manager handles the reconnection logic.
A single Manager can be used by several Sockets.

A Socket is the fundamental class for interacting with the server. A Socket belongs to a certain Namespace (by default /) and uses an underlying Manager to communicate.

Basically, the manager instance is implicitly created when running io(). It can be accessed with the io attribute of the Socket, and will be reused if you create another Socket (unless you use the forceNew option):

const socket = io();
const socket2 = io("/test2");
// socket.io === socket2.io
const socket3 = io("/test3", { forceNew: true }); // new manager
// socket.io !== socket3.io

Regarding the events, the following events are related to the state of the connection and will be emitted by both the Manager and its associated Sockets:

  • connect_error
  • connect_timeout
  • reconnect
  • reconnecting
  • reconnect_error
  • reconnect_failed
  • ping
  • pong

The Socket will emit these additional events, which are related to the Namespace:

  • connect
  • disconnect
  • error

Please tell me if that's clear enough. And sorry for the delay!

@darrachequesne Thank you for your explanation, aslo can you explan a little bit the differece between io(url, options) and
io.connect(url, options) please? The doc says they both return a socket instance, but how can we use them differently?

metoo

I'd love to know the difference, if there's one, between:

  • io(url, options)
  • io.connect(url, options)

And what's the pourpose of:

  • new Manager(url, options)

I can't seem to find anything usable in the documentation apart a very detailed but cold description of the parameters.

Also, if I can use in my browser's client html page either socket.io or socket.io-client

Thank you and sorry, I see lots of work has been put on this library but it's very confusing to get an hang of it.

I think both io(url, options) and io.connect(url, options) are equal:

// https://github.com/socketio/socket.io-client/blob/2.3.0/lib/index.js#L15
module.exports = exports = lookup;

// https://github.com/socketio/socket.io-client/blob/2.3.0/lib/index.js#L85
exports.connect = lookup;

Now, why are there two different ways of doing the same thing, that's a good question... :smile: . It seems to be like that since 2012: https://github.com/socketio/socket.io-client/commit/d5652feadc1a2085942b5a6a22394f07242e77b2. I'll check if there are specific reasons for that, but else the connect() could be removed in v3.

Regarding the manager constructor, maybe it should be removed from the public API. Basically:

const socket = io("ws://example.com/my-namespace", {
  reconnectionDelayMax: 10000,
  query: {
    auth: "123"
  }
});

// is the same as
const manager = new Manager("ws://example.com", {
  reconnectionDelayMax: 10000
});
const socket = manager.socket("/my-namespace", {
  query: {
    auth: "123"
  }
});

Having access to the manager makes a lot of sense, so this way you can have full control of the socket instances instead of using the global default manager that the package holds underneath which could also be shared by another package f.e..

Was this page helpful?
0 / 5 - 0 ratings

Related issues

najibghadri picture najibghadri  ·  7Comments

ahadcove picture ahadcove  ·  7Comments

exilonX picture exilonX  ·  7Comments

patrickbussmann picture patrickbussmann  ·  6Comments

catamphetamine picture catamphetamine  ·  3Comments