Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: socketio/socket.io
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3.1.2
Choose a base ref
...
head repository: socketio/socket.io
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 4.0.0
Choose a head ref
  • 7 commits
  • 19 files changed
  • 3 contributors

Commits on Mar 1, 2021

  1. 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:
    
    - #3629
    - #3657
    sebamarynissen authored and darrachequesne committed Mar 1, 2021

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    darrachequesne Damien Arrachequesne
    Copy the full SHA
    7de2e87 View commit details
  2. 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:
    
    - #3431
    - #3444
    darrachequesne committed Mar 1, 2021

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    darrachequesne Damien Arrachequesne
    Copy the full SHA
    ac9e8ca View commit details
  3. 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: #3048
    darrachequesne committed Mar 1, 2021

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    darrachequesne Damien Arrachequesne
    Copy the full SHA
    085d1de View commit details

Commits on Mar 2, 2021

  1. 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:
    
    - #3042
    - #3418
    - #3570
    - socketio/socket.io-redis-adapter#283
    darrachequesne committed Mar 2, 2021

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    darrachequesne Damien Arrachequesne
    Copy the full SHA
    b25495c View commit details

Commits on Mar 9, 2021

  1. 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: #3742
    
    [1]: https://github.com/binier/tiny-typed-emitter
    MaximeKjaer authored and darrachequesne committed Mar 9, 2021

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    darrachequesne Damien Arrachequesne
    Copy the full SHA
    0107510 View commit details

Commits on Mar 10, 2021

  1. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    darrachequesne Damien Arrachequesne
    Copy the full SHA
    1b6d6de View commit details
  2. chore(release): 4.0.0

    darrachequesne committed Mar 10, 2021

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    darrachequesne Damien Arrachequesne
    Copy the full SHA
    5eaeffc View commit details
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# [4.0.0](https://github.com/socketio/socket.io/compare/3.1.2...4.0.0) (2021-03-10)


### Bug Fixes

* make io.to(...) immutable ([ac9e8ca](https://github.com/socketio/socket.io/commit/ac9e8ca6c71e00d4af45ee03f590fe56f3951186))


### Features

* add some utility methods ([b25495c](https://github.com/socketio/socket.io/commit/b25495c069031674da08e19aed68922c7c7a0e28))
* add support for typed events ([#3822](https://github.com/socketio/socket.io/issues/3822)) ([0107510](https://github.com/socketio/socket.io/commit/0107510ba8a0f148c78029d8be8919b350feb633))
* allow to exclude specific rooms when broadcasting ([#3789](https://github.com/socketio/socket.io/issues/3789)) ([7de2e87](https://github.com/socketio/socket.io/commit/7de2e87e888d849eb2dfc5e362af4c9e86044701))
* allow to pass an array to io.to(...) ([085d1de](https://github.com/socketio/socket.io/commit/085d1de9df909651de8b313cc6f9f253374b702e))


## [3.1.2](https://github.com/socketio/socket.io/compare/3.1.1...3.1.2) (2021-02-26)


Loading