62

Does anyone know what events are built-in in Socket.io?
For example: connection, disconnect, join etc.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
Abhishek Kaushik
  • 1,121
  • 1
  • 13
  • 17

1 Answers1

134

Here is all I found in the official docs:

Client-side events for socket.io object:

  • connect. Fired upon a successful connection.

  • connect_error. Fired upon a connection error.
    Parameters:
    • Object error object

  • connect_timeout. Fired upon a connection timeout.

  • reconnect. Fired upon a successful reconnection.
    Parameters:
    • Number reconnection attempt number

  • reconnect_attempt. Fired upon an attempt to reconnect.

  • reconnecting. Fired upon an attempt to reconnect.
    Parameters:
    • Number reconnection attempt number

  • reconnect_error. Fired upon a reconnection attempt error.
    Parameters:
    • Object error object

  • reconnect_failed. Fired when couldn’t reconnect within reconnectionAttempts

Client-side events for socket object:

  • connect. Fired upon connecting.
  • error. Fired upon a connection error
    Parameters:
    • Object error data
  • disconnect. Fired upon a disconnection.
  • reconnect. Fired upon a successful reconnection.
    Parameters:
    • Number reconnection attempt number
  • reconnect_attempt. Fired upon an attempt to reconnect.
  • reconnecting. Fired upon an attempt to reconnect.
    Parameters:
    • Number reconnection attempt number
  • reconnect_error. Fired upon a reconnection attempt error.
    Parameters:
    • Object error object
  • reconnect_failed. Fired when couldn’t reconnect within reconnectionAttempts

Server-side events:

  • connection / connect. Fired upon a connection.
    Parameters:
    • Socket the incoming socket.

Edit:

For the current version (1.3.4) the reconnect_attempt and reconnecting client-side events are synonyms.

skellertor
  • 916
  • 1
  • 9
  • 26
Oleg
  • 22,300
  • 9
  • 68
  • 84
  • 7
    Hmm, that can't be it, as it doesn't even list "disconnect". It seems the documentation is very incomplete. Does anybody have a proper list? – Killroy Jan 08 '15 at 15:14
  • @Killroy I added client `socket` events to my answer. However I can't find server `socket` events in the docs and other places, unfortunately. – Oleg Feb 19 '15 at 16:57
  • Hmm, I think the same socket set up is used client and server. So "client-side" is misleading, as it seems the same socket events are fired on the server end as well. I'm still hoping to get a definitive answer, perhaps from the source? Unfortunately sting-based events are tricky to track in a complex project. – Killroy Feb 19 '15 at 17:34
  • 1
    @Killroy After exploring socket.io source code for awhile I came to the conclusion that server-side socket differs from the client-side one. For example, it doesn't have `reconnect`, `reconnect_attempt`, `reconnecting`, `reconnect_error`, `reconnect_failed` events, because the new server-side socket is created on each reconnection. That's all I can say by this time. I'll let you know if I find out more. – Oleg Feb 19 '15 at 20:05
  • Hi Curious. I awarded the bounty for the best answer. I am not 100% convinced though that it is 100% reliable, as I don't know if you verified them all in the source. – Killroy Feb 26 '15 at 15:25
  • @Killroy Thank you for the bounty. As for source code, I checked it and it seems to reflect the documentation. Also see my edit. However, socket.io versions are released very often, there is a lot of contributors, and its API may be changed in the future. – Oleg Feb 26 '15 at 16:51
  • 13
    There is also a server-side 'disconnect' event (verified working in 1.3.5), though I can't actually find documentation for it anywhere. The docs are so abstract from usage though (*cough* terrible *cough*) that I might just be looking at the wrong thing. – gregmac Jun 10 '15 at 14:39
  • is there an event named 'rooms' in socket.io too? need help with this question [here is the question](http://stackoverflow.com/questions/36553889/how-does-event-emitter-pattern-work-in-node-sockets-io) – level0 Apr 11 '16 at 20:56
  • 1
    IMPORTANT! There are more events, however we can all conclude that the docs suck.. This issue (https://github.com/socketio/socket.io/issues/1814) regarding a new event (that took 2 years to officially implement) - and it is nowhere to be found. However, it is there - the event `disconnecting` can be useful. – JakeTheSnake Oct 02 '17 at 14:54
  • 2
    Why didn't they just add a class containing all the constants? :facepalm: It would have been much easier – TheRealChx101 Nov 19 '19 at 18:46
  • 1
    Hi! nowadays the correct link to the docs must be, for client side https://socket.io/docs/v4/client-options/#manager-options and for server side https://socket.io/docs/v4/server-api/ – Julian Porras Jun 16 '22 at 16:59