Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What's the difference between io.Manager and io.Socket? #1214

Closed
bfang711 opened this issue Jun 22, 2018 · 14 comments
Closed

What's the difference between io.Manager and io.Socket? #1214

bfang711 opened this issue Jun 22, 2018 · 14 comments
Labels
question Further information is requested

Comments

@bfang711
Copy link

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.

@SebastianTroc
Copy link

+1

1 similar comment
@Prateek327
Copy link

+1

@ozyman42
Copy link

ozyman42 commented Apr 9, 2019

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.

@flyinghawker
Copy link

flyinghawker commented Apr 9, 2019

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.

@DrLightman
Copy link

#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.

@vivekkhimani
Copy link

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

@angusmunro
Copy link

+1,

@shlomisas
Copy link

+1

@gdscfuto
Copy link

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

@darrachequesne
Copy link
Member

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!

@davuses
Copy link

davuses commented Sep 29, 2020

@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.

@darrachequesne
Copy link
Member

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... 😄 . It seems to be like that since 2012: d5652fe. 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"
  }
});

darrachequesne added a commit to socketio/socket.io-website that referenced this issue Sep 29, 2020
@darrachequesne darrachequesne added the question Further information is requested label Oct 1, 2020
@darrachequesne
Copy link
Member

I've updated the documentation: socketio/socket.io-website@e779468

Hope that's clearer now!

@mgtitimoli
Copy link

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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

14 participants