Skip to content

Commit

Permalink
feat: add details to the disconnect event
Browse files Browse the repository at this point in the history
The "disconnect" event will now include additional details to help
debugging if anything has gone wrong.

Example when a payload is over the maxHttpBufferSize value in HTTP
long-polling mode:

```js
socket.on("disconnect", (reason, details) => {
  console.log(reason); // "transport error"

  // in that case, details is an error object
  console.log(details.message); "xhr post error"
  console.log(details.description); // 413 (the HTTP status of the response)

  // details.context refers to the XMLHttpRequest object
  console.log(details.context.status); // 413
  console.log(details.context.responseText); // ""
});
```

Related: socketio/engine.io-client@b9252e2
  • Loading branch information
darrachequesne committed Apr 22, 2022
1 parent eaf782c commit b862924
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 105 deletions.
8 changes: 4 additions & 4 deletions lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
SocketOptions as EngineOptions,
installTimerFunctions,
} from "engine.io-client";
import { Socket, SocketOptions } from "./socket.js";
import { Socket, SocketOptions, DisconnectDescription } from "./socket.js";
import * as parser from "socket.io-parser";
import { Decoder, Encoder, Packet } from "socket.io-parser";
import { on } from "./on.js";
Expand Down Expand Up @@ -90,7 +90,7 @@ interface ManagerReservedEvents {
error: (err: Error) => void;
ping: () => void;
packet: (packet: Packet) => void;
close: (reason: string) => void;
close: (reason: string, description?: DisconnectDescription) => void;
reconnect_failed: () => void;
reconnect_attempt: (attempt: number) => void;
reconnect_error: (err: Error) => void;
Expand Down Expand Up @@ -538,13 +538,13 @@ export class Manager<
*
* @private
*/
private onclose(reason: string): void {
private onclose(reason: string, description?: DisconnectDescription): void {
debug("closed due to %s", reason);

this.cleanup();
this.backoff.reset();
this._readyState = "closed";
this.emitReserved("close", reason);
this.emitReserved("close", reason, description);

if (this._reconnection && !this.skipReconnect) {
this.reconnect();
Expand Down
20 changes: 17 additions & 3 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@ interface Flags {
timeout?: number;
}

export type DisconnectDescription =
| Error
| {
description: string;
context?: CloseEvent | XMLHttpRequest;
};

interface SocketReservedEvents {
connect: () => void;
connect_error: (err: Error) => void;
disconnect: (reason: Socket.DisconnectReason) => void;
disconnect: (
reason: Socket.DisconnectReason,
description?: DisconnectDescription
) => void;
}

export class Socket<
Expand Down Expand Up @@ -266,14 +276,18 @@ export class Socket<
* Called upon engine `close`.
*
* @param reason
* @param description
* @private
*/
private onclose(reason: Socket.DisconnectReason): void {
private onclose(
reason: Socket.DisconnectReason,
description?: DisconnectDescription
): void {
debug("close (%s)", reason);
this.connected = false;
this.disconnected = true;
delete this.id;
this.emitReserved("disconnect", reason);
this.emitReserved("disconnect", reason, description);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/url.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import parseuri from "parseuri";
import { parse } from "engine.io-client";
import debugModule from "debug"; // debug()

const debug = debugModule("socket.io-client:url"); // debug()
Expand Down Expand Up @@ -68,7 +68,7 @@ export function url(

// parse
debug("parse %s", uri);
obj = parseuri(uri) as ParsedUrl;
obj = parse(uri) as ParsedUrl;
}

// make sure we treat `localhost:80` and `localhost` equally
Expand Down
147 changes: 55 additions & 92 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@
},
"types": "./build/esm/index.d.ts",
"dependencies": {
"@socket.io/component-emitter": "~3.0.0",
"@socket.io/component-emitter": "~3.1.0",
"backo2": "~1.0.2",
"debug": "~4.3.2",
"engine.io-client": "~6.1.1",
"parseuri": "0.0.6",
"socket.io-parser": "~4.1.1"
"engine.io-client": "~6.2.1",
"socket.io-parser": "~4.2.0"
},
"devDependencies": {
"@babel/core": "^7.15.0",
Expand Down

1 comment on commit b862924

@redefinered-c
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we implement the same thing with the server side library?

Please sign in to comment.