Commit Graph

7693 Commits

Author SHA1 Message Date
Damien Arrachequesne
5eaeffc8e2 chore(release): 4.0.0
Diff: https://github.com/socketio/socket.io/compare/3.1.2...4.0.0
4.0.0
2021-03-10 12:43:53 +01:00
Damien Arrachequesne
d28cde7afc chore(release): 4.0.0
The major bump is due to some breaking changes on the server side.

Diff: https://github.com/socketio/socket.io-client/compare/3.1.2...4.0.0
2021-03-10 12:25:27 +01:00
Damien Arrachequesne
43613d1b2c fix(bundle): restore support for JS modules
This change is needed so the bundle can be used with:

```
<script type="module" src="xxxx/socket.io.js"></script>
```

Related:

- https://github.com/socketio/socket.io/discussions/3828
- 13b32b39a4
- 8c08c5d5c3
2021-03-10 12:17:52 +01:00
KC Erb
6abfa1fa4c feat: add autoUnref option
With autoUnref set to true (default: false), the Socket.IO client will
allow the program to exit if there is no other active timer/socket in
the event system.

```js
const socket = io({
  autoUnref: true
});
```

Note: this option only applies to Node.js clients.

Related: https://github.com/socketio/socket.io-client/issues/1446
2021-03-10 12:16:12 +01:00
Damien Arrachequesne
1b6d6de4ed chore: include Engine.IO v5
Release notes: https://github.com/socketio/engine.io/releases/tag/5.0.0
2021-03-10 11:14:33 +01:00
Damien Arrachequesne
887ba06536 chore(release): 5.0.0
Diff: https://github.com/socketio/engine.io/compare/4.1.1...5.0.0
2021-03-10 10:20:02 +01:00
Damien Arrachequesne
14e81805ab chore(release): 5.0.0
The major bump is due to a breaking change on the server side.

Diff: https://github.com/socketio/engine.io-client/compare/4.1.2...5.0.0
2021-03-10 10:09:12 +01:00
Damien Arrachequesne
b8e06a8674 refactor: use globalObject "self" in webpack configuration 2021-03-10 10:05:36 +01:00
Damien Arrachequesne
59023657a0 feat: add support for typed events
Syntax:

```ts
interface ServerToClientEvents {
  "my-event": (a: number, b: string, c: number[]) => void;
}

interface ClientToServerEvents {
  hello: (message: string) => void;
}

const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io();

socket.emit("hello", "world");

socket.on("my-event", (a, b, c) => {
  // ...
});
```

The events are not typed by default (inferred as any), so this change
is backward compatible.

Related: https://github.com/socketio/socket.io/issues/3742
2021-03-10 01:24:56 +01:00
Maxime Kjaer
0107510ba8 feat: add support for typed events (#3822)
Syntax:

```ts
interface ClientToServerEvents {
  "my-event": (a: number, b: string, c: number[]) => void;
}

interface ServerToClientEvents {
  hello: (message: string) => void;
}

const io = new Server<ClientToServerEvents, ServerToClientEvents>(httpServer);

io.emit("hello", "world");

io.on("connection", (socket) => {
  socket.on("my-event", (a, b, c) => {
    // ...
  });

  socket.emit("hello", "again");
});
```

The events are not typed by default (inferred as any), so this change
is backward compatible.

Note: we could also have reused the method here ([1]) to add types to
the EventEmitter, instead of creating a StrictEventEmitter class.

Related: https://github.com/socketio/socket.io/issues/3742

[1]: https://github.com/binier/tiny-typed-emitter
2021-03-10 00:18:13 +01:00
Damien Arrachequesne
edb734316f feat: remove dynamic require() with wsEngine
This change is necessary to get rid of:

> Critical dependency: the request of a dependency is an expression

when bundling the server with webpack.

BREAKING CHANGE: the syntax of the "wsEngine" option is updated

Before:

```js
const eioServer = require("engine.io")(httpServer, {
  wsEngine: "eiows"
});
```

After:

```js
const eioServer = require("engine.io")(httpServer, {
  wsEngine: require("eiows").Server
});
```

Related: https://github.com/socketio/engine.io/issues/609
2021-03-09 08:30:05 +01:00
Simone Mazzoni
868d89111d fix: set default protocol version to 3 (#616)
socket.io-client-swift libs version <=15.2.0, which uses protocol
version 3, do not explicitly add the EIO query parameter at transport
initialization. This omission leads the server to treat the client as
a client that supports the protocol version 4, previously set as
default, which is not correct for those versions of the client lib.

From socket.io-client-swift version v16.0.0 the EIO query parameter is
explicitly passed to specify the protocol version supported, but since
the allowEIO3 parameter aims to make the server compatible with
previous versions which in most of the cases are already used in
production and not easily upgradable, it makes more sense to default
the EIO version to 3 if not explicitly set by the client since the
newer client versions pass the EIO protocol version in query
parameters.

Related: https://github.com/socketio/socket.io/issues/3794
2021-03-09 08:23:36 +01:00
Damien Arrachequesne
65516836b2 feat: add autoUnref option
With autoUnref set to true (default: false), the Engine.IO client will
allow the program to exit if there is no other active timer/socket in
the event system.

Note: the 'xmlhttprequest-ssl' package has been copied in the contrib/
directory, until the change is merged upstream

Related: https://github.com/socketio/engine.io-client/issues/653
2021-03-03 10:12:40 +01:00
Bastian Kersting
063136b91d docs: fix typo (#36) 2021-03-02 12:40:47 +01:00
Damien Arrachequesne
b25495c069 feat: add some utility methods
This commit adds the following methods:

- fetchSockets: returns the matching socket instances

Syntax:

```js
// return all Socket instances
const sockets = await io.fetchSockets();

// return all Socket instances of the "admin" namespace in the "room1" room
const sockets = await io.of("/admin").in("room1").fetchSockets();
```

- socketsJoin: makes the matching socket instances join the specified rooms

Syntax:

```js
// make all Socket instances join the "room1" room
io.socketsJoin("room1");

// make all Socket instances of the "admin" namespace in the "room1" room join the "room2" room
io.of("/admin").in("room1").socketsJoin("room2");
```

- socketsLeave: makes the matching socket instances leave the specified rooms

Syntax:

```js
// make all Socket instances leave the "room1" room
io.socketsLeave("room1");

// make all Socket instances of the "admin" namespace in the "room1" room leave the "room2" room
io.of("/admin").in("room1").socketsLeave("room2");
```

- disconnectSockets: makes the matching socket instances disconnect

Syntax:

```js
// make all Socket instances disconnect
io.disconnectSockets();

// make all Socket instances of the "admin" namespace in the "room1" room disconnect
io.of("/admin").in("room1").disconnectSockets();
```

Those methods share the same semantics as broadcasting. They will also
work with multiple Socket.IO servers when using the Redis adapter. In
that case, the fetchSockets() method will return a list of RemoteSocket
instances, which expose a subset of the methods and attributes of the
Socket class (the "request" attribute cannot be mocked, for example).

Related:

- https://github.com/socketio/socket.io/issues/3042
- https://github.com/socketio/socket.io/issues/3418
- https://github.com/socketio/socket.io/issues/3570
- https://github.com/socketio/socket.io-redis/issues/283
2021-03-02 11:17:21 +01:00
Damien Arrachequesne
9bf70c17bd docs(changelog): include changelog for release 3.5.1
Merged from the 3.5.x branch.
2021-03-02 09:45:18 +01:00
Damien Arrachequesne
c361bc691f feat: listen to the "offline" event
The connection will be closed once the "offline" event is emitted by
the browser, in order not to wait for the heartbeat mechanism to detect
the disconnection.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event

Related: https://github.com/socketio/socket.io-client/issues/1433
2021-03-02 09:24:21 +01:00
Damien Arrachequesne
5a7fa132c4 feat: increase the default value of pingTimeout
This value was updated from 60000 to 5000 in [1], included in
`engine.io@3.2.0` (Feb 2018).

The reasoning back then:

Some users experienced long delays between disconnection on the
server-side and on the client-side. The "disconnect" event would take a
long time to fire in the browser, probably due to a timer being
delayed. Hence the change.

That being said, the current value (5s) now causes unexpected
disconnections when a big payload is sent over a slow network, because
it prevents the ping-pong packets from being exchanged between the
client and the server. This can also happen when a synchronous task
blocks the server for more than 5 seconds.

The new value (20s) thus seems like a good balance between quick
disconnection detection and tolerance to various delays.

Note: pingInterval + pingTimeout is still below the threshold of React
Native, which complains if a timer is set with a delay of more than 1
minute.

[1]: 65b1ad1b8a

Related:

- https://github.com/socketio/socket.io/issues/2770
- https://github.com/socketio/socket.io/issues/2769
- https://github.com/socketio/socket.io/issues/3054
- https://github.com/socketio/socket.io/issues/3376
2021-03-02 08:49:58 +01:00
Damien Arrachequesne
085d1de9df feat: allow to pass an array to io.to(...)
In some cases it is necessary to pass an array of rooms instead of a single room.

New syntax:

```
io.to(["room1", "room2"]).except(["room3"]).emit(...);

socket.to(["room1", "room2"]).except(["room3"]).emit(...);
```

Related: https://github.com/socketio/socket.io/issues/3048
2021-03-01 23:20:46 +01:00
Damien Arrachequesne
ac9e8ca6c7 fix: make io.to(...) immutable
Previously, broadcasting to a given room (by calling `io.to()`) would
mutate the io instance, which could lead to surprising behaviors, like:

```js
io.to("room1");
io.to("room2").emit(...); // also sent to room1

// or with async/await
io.to("room3").emit("details", await fetchDetails()); // random behavior: maybe in room3, maybe to all clients
```

Calling `io.to()` (or any other broadcast modifier) will now return an
immutable instance.

Related:

- https://github.com/socketio/socket.io/issues/3431
- https://github.com/socketio/socket.io/issues/3444
2021-03-01 23:17:08 +01:00
Sebastiaan Marynissen
7de2e87e88 feat: allow to exclude specific rooms when broadcasting (#3789)
New syntax:

```
io.except("room1").emit(...);
io.to("room1").except("room2").emit(...);

socket.broadcast.except("room1").emit(...);
socket.to("room1").except("room2").emit(...);
```

Related:

- https://github.com/socketio/socket.io/issues/3629
- https://github.com/socketio/socket.io/issues/3657
2021-03-01 09:30:58 +01:00
Damien Arrachequesne
c679e62236 chore(release): 2.2.0
Diff: https://github.com/socketio/socket.io-adapter/compare/2.1.0...2.2.0
2021-02-27 02:08:54 +01:00
Damien Arrachequesne
1c9827ec11 feat: add some utility methods
This commit adds the following methods:

- fetchSockets: return the matching socket instances
- addSockets: make the matching socket instances join the specified rooms
- delSockets: make the matching socket instances leave the specified rooms
- disconnectSockets: disconnect the matching socket instances

Those methods will then be exposed by the Socket.IO server:

```js
// clear room
io.socketsLeave("room1");

// disconnect all sockets in room
io.in("room2").disconnectSockets();

// fetch socket instances in room
io.in("room3").fetchSockets();
```

This feature will also be extended in the Redis adapter to handle
multiple Socket.IO servers.
2021-02-27 01:37:53 +01:00
Sebastiaan Marynissen
985bb41fa2 feat: allow excluding all sockets in a room (#66) 2021-02-26 15:32:45 +01:00
Damien Arrachequesne
225ade062a chore(release): 3.1.2
Diff: https://github.com/socketio/socket.io/compare/3.1.1...3.1.2
3.1.2
2021-02-26 01:18:42 +01:00
Damien Arrachequesne
78ec5a6e43 chore(release): 3.1.2
Diff: https://github.com/socketio/socket.io-client/compare/3.1.1...3.1.2
2021-02-26 01:10:30 +01:00
Damien Arrachequesne
83a65be78a chore: bump engine.io-client version
Diff: https://github.com/socketio/engine.io-client/compare/4.1.0...4.1.2
2021-02-26 01:08:39 +01:00
Damien Arrachequesne
494c64e44f fix: ignore packet received after disconnection
Related: https://github.com/socketio/socket.io/issues/3095
2021-02-26 00:58:30 +01:00
Damien Arrachequesne
67a61e39e6 chore: loosen the version requirement of @types/node
Related: https://github.com/socketio/socket.io/issues/3793
2021-02-26 00:58:01 +01:00
Damien Arrachequesne
13b32b39a4 fix: restore support for web workers
A previous change ([1], included in 3.0.5) broke support for web
workers, which threw "window is not defined" when importing the bundle
in a web worker.

Related: https://github.com/socketio/socket.io/issues/3809

Reference: https://webpack.js.org/configuration/output/#outputglobalobject

[1]: 8c08c5d5c3
2021-02-26 00:43:26 +01:00
Damien Arrachequesne
6f7c89db76 chore(release): 4.1.2
Diff: https://github.com/socketio/engine.io-client/compare/4.1.1...4.1.2
2021-02-25 22:42:51 +01:00
Damien Arrachequesne
ed48b5dc34 fix: silently close the transport in the beforeunload hook
Related:

- https://github.com/socketio/socket.io/issues/3639
- https://github.com/socketio/socket.io/issues/3069
2021-02-25 00:32:18 +01:00
Damien Arrachequesne
76338d79fb refactor: use globalObject "this" in webpack configuration 2021-02-25 00:29:37 +01:00
Damien Arrachequesne
7467216e02 docs(examples): 4th and final part of the "private messaging" example
See also: https://socket.io/get-started/private-messaging-part-4/
2021-02-17 00:24:23 +01:00
Damien Arrachequesne
7247b4051f docs(examples): 3rd part of the "private messaging" example
See also: https://socket.io/get-started/private-messaging-part-3/
2021-02-17 00:22:55 +01:00
Damien Arrachequesne
992c9380c3 docs(examples): 2nd part of the "private messaging" example
See also: https://socket.io/get-started/private-messaging-part-2/
2021-02-15 01:09:12 +01:00
Damien Arrachequesne
8b404f424b docs(examples): 1st part of the "private messaging" example 2021-02-10 00:33:39 +01:00
Damien Arrachequesne
12221f296d chore(release): 3.1.1
Diff: https://github.com/socketio/socket.io/compare/3.1.0...3.1.1
3.1.1
2021-02-03 22:57:21 +01:00
Damien Arrachequesne
6f4bd7f8e7 fix: properly parse the CONNECT packet in v2 compatibility mode
In Socket.IO v2, the Socket query option was appended to the namespace
in the CONNECT packet:

{
  type: 0,
  nsp: "/my-namespace?abc=123"
}

Note: the "query" option on the client-side (v2) will be found in the
"auth" attribute on the server-side:

```
// client-side
const socket = io("/nsp1", {
  query: {
    abc: 123
  }
});
socket.query = { abc: 456 };

// server-side
const io = require("socket.io")(httpServer, {
  allowEIO3: true // enable compatibility mode
});

io.of("/nsp1").on("connection", (socket) => {
  console.log(socket.handshake.auth); // { abc: 456 } (the Socket query)
  console.log(socket.handshake.query.abc); // 123 (the Manager query)
});

More information here: https://socket.io/docs/v3/migrating-from-2-x-to-3-0/#Add-a-clear-distinction-between-the-Manager-query-option-and-the-Socket-query-option

Related: https://github.com/socketio/socket.io/issues/3791
2021-02-03 22:54:07 +01:00
Damien Arrachequesne
4f2e9a716d fix(typings): update the types of "query", "auth" and "headers"
Related: https://github.com/socketio/socket.io/issues/3770
2021-02-03 22:53:38 +01:00
Damien Arrachequesne
311c5d14c3 chore(release): 3.1.1
Diff: https://github.com/socketio/socket.io-client/compare/3.1.0...3.1.1
2021-02-03 22:36:17 +01:00
Damien Arrachequesne
7a0c2b504f fix: include the path in the manager ID
Previously, the following code:

```js
const socket1 = io({
  path: "/test1"
});
const socket2 = io({
  path: "/test2"
});
```

would result in one single Manager, with the "/test2" path being
silently ignored.

Two distinct Manager instances will now be created.

Related: https://github.com/socketio/socket.io-client/issues/1225
2021-02-03 22:31:01 +01:00
Damien Arrachequesne
61afc5d8cb fix: remove polyfill for process in the bundle
A polyfill for Node.js "process" was included in the final bundle.

Reference: https://webpack.js.org/configuration/node/
2021-02-02 12:08:26 +01:00
david-fong
9e8f288ca9 fix(typings): add return types and general-case overload signatures (#3776)
See also: https://stackoverflow.com/questions/52760509/typescript-returntype-of-overloaded-function/52760599#52760599
2021-02-02 11:50:08 +01:00
david-fong
47f917afdd fix(typings): add return types and general-case overload signatures (#1440) 2021-02-02 11:29:08 +01:00
Mick Lawitzke
f02ab3bc96 fix(typings): fix the type of the "query" option (#1439)
Having type `Object` it was not possible to set values, e.g.:

```ts
if (!this.socket.io.opts.query) {
  this.socket.io.opts.query = {};
}
this.socket.io.opts.query.token = 'abc123';
```

Results in error:

> Element implicitly has an 'any' type because expression of type '"token"' can't be used to index type 'Object'.
2021-02-02 11:26:33 +01:00
Damien Arrachequesne
9534355b89 chore(release): 4.1.1
Diff: https://github.com/socketio/engine.io/compare/4.1.0...4.1.1
2021-02-02 10:51:04 +01:00
Damien Arrachequesne
ae840fa8ed chore: point towards the master branch for the CI badge
Reference: https://docs.github.com/en/actions/managing-workflow-runs/adding-a-workflow-status-badge#using-the-branch-parameter
2021-02-02 10:49:24 +01:00
Damien Arrachequesne
ff2b8aba48 fix: do not reset the ping timer after upgrade
There was two issues with this behavior:

- v3 clients (with allowEIO3: true) were also receiving a "ping" after
a successful upgrade, which is incorrect (in v3, it's the client that
sends the "ping", and the server answers with a "pong")

- the ping timer is not reset after upgrade on the client-side, so an
upgrade which took longer than the `pingTimeout` duration could lead to
a "ping timeout" error on the client-side

I think the latter issue is present since the initial implementation.

Related: https://github.com/socketio/socket.io-client-swift/pull/1309#issuecomment-768475704
2021-02-02 10:48:02 +01:00
Damien Arrachequesne
ee0a35d0e4 chore(release): 4.1.1
Diff: https://github.com/socketio/engine.io-client/compare/4.1.0...4.1.1
2021-02-02 09:59:44 +01:00