Skip to content

Conversation

@NikhilPatil24
Copy link

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

New behavior

Other information (e.g. related issues)

darrachequesne and others added 30 commits January 3, 2024 21:25
Before this change, the session and user context were retrieved once
per HTTP request and not once per session.
This change is necessary to be able to write "import type { ... }".
When compiling with TypeScript with module set to "node16" and
moduleResolution to "node16", the following error would be thrown:

> node_modules/engine.io-parser/build/cjs/index.d.ts:6:54 - error TS2304: Cannot find name 'TransformStream'.
> 6 export declare function createPacketEncoderStream(): TransformStream<Packet, any>;
>                                                        ~~~~~~~~~~~~~~~
> node_modules/engine.io-parser/build/cjs/index.d.ts:7:96 - error TS2304: Cannot find name 'TransformStream'.
> 7 export declare function createPacketDecoderStream(maxPayload: number, binaryType: BinaryType): TransformStream<Uint8Array, any>;
>                                                                                                  ~~~~~~~~~~~~~~~
> Found 2 errors in the same file, starting at: node_modules/engine.io-parser/build/cjs/index.d.ts:6

This is because the TransformStream object is not exposed in the global
scope in the `@types/node` package, even though it is since Node.js
`v18.0.0`.

Reference: https://nodejs.org/api/webstreams.html#class-transformstream

Note: we only import the TransformStream type (not value) because it
isn't defined on older Node.js versions.

Related:

- https://github.com/socketio/engine.io-parser/issues/136
- socketio/socket.io-client#1606
Imported from https://github.com/socketio/socket.io-redis-adapter/blob/ef5f0da0b4928fd422afc985aec0e233d34400c0/lib/cluster-adapter.ts

This abstract class is currently used by the sharded Redis adapter. We
import it here because the logic can be reused by the other adapters,
which will then only need to handle the pub/sub mechanism.
The clustered adapter will now:

- periodically check if a node has left the cluster
- send an event when it leaves the cluster

This should reduce the number of "timeout reached: only x responses
received out of y" errors.
The Redis adapter is currently the only adapter which makes a
distinction between publishing messages (one channel for all) and
responses (one channel for each node).
Note: the current API does not currently allow the user to handle those
errors (and retry, for example). This might be worth investigating for
the next major version, maybe something like:

```js
try {
  await io.emit("hello");
} catch (e) {
  // something went wrong
}
```

Related: socketio/socket.io-mongo-adapter#15
darrachequesne and others added 30 commits January 23, 2025 07:38
This reverts commit 7427109.

The new version of the `cookie` package contains code with optional chaining (`?.`), which is not supported by older Node.js versions (< 14).

The types for cookie are now bundled, so that there is no conflict with the types coming from `cookie@1`:

> error TS2724: '"cookie"' has no exported member named 'CookieSerializeOptions'. Did you mean 'SerializeOptions'?
>
> import type { CookieSerializeOptions } from "cookie";
>               ~~~~~~~~~~~~~~~~~~~~~~

Related: socketio#5283
Passing { port: "443" } would include the port in the URL (":443").
The init() method of the adapter will now be called when creating a namespace with `io.of(<the-namespace>)`.

Note: any promise rejection is silently caught, as I don't see how we could properly expose the promise.

```js
const io = new Server({
  adapter: myAdapter
});
// under the hood, this:
// - implicitly creates the main namespace (/)
// - creates an instance of `myAdapter` for the main namespace
// - calls `myAdapter.init()` (with this change)
```

Related:

- socketio#3662
- socketio/socket.io-postgres-adapter#16
The ClusterAdapter class has been moved to [1], so that this adapter
only needs to implement to pub/sub mechanism.

Also, [2] should reduce the number of "timeout reached: only x
responses received out of y" errors, since the fetchSockets() requests
will now succeed even if a server leaves the cluster.

[1]: https://github.com/socketio/socket.io/tree/main/packages/socket.io-adapter
[2]: socketio@0e23ff0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.