Skip to content

Commit

Permalink
perf: precompute the WebSocket frames when broadcasting
Browse files Browse the repository at this point in the history
Note:

- only packets without binary attachments are affected
- the permessage-deflate extension must be disabled (which is the default)

Related:

- socketio/socket.io-adapter@5f7b47d
- socketio/engine.io@5e34722
  • Loading branch information
darrachequesne committed Jan 12, 2023
1 parent b7d54db commit da2b542
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 70 deletions.
172 changes: 108 additions & 64 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"accepts": "~1.3.4",
"base64id": "~2.0.0",
"debug": "~4.3.2",
"engine.io": "~6.2.1",
"socket.io-adapter": "~2.4.0",
"engine.io": "~6.3.1",
"socket.io-adapter": "~2.5.2",
"socket.io-parser": "~4.2.1"
},
"devDependencies": {
Expand Down
18 changes: 18 additions & 0 deletions test/messaging-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,22 @@ describe("messaging many", () => {
success(done, io, socket1, socket2, socket3);
});
});

it("should precompute the WebSocket frame when broadcasting", (done) => {
const io = new Server(0);
const socket = createClient(io, "/chat", {
transports: ["websocket"],
});
const partialDone = createPartialDone(2, successFn(done, io, socket));

io.of("/chat").on("connection", (s) => {
s.conn.once("packetCreate", (packet) => {
expect(packet.options.wsPreEncodedFrame).to.be.an(Array);
partialDone();
});
io.of("/chat").compress(false).emit("woot", "hi");
});

socket.on("woot", partialDone);
});
});
8 changes: 4 additions & 4 deletions test/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ describe("socket", () => {
});
});

it("should enable compresion by default", (done) => {
it("should enable compression by default", (done) => {
const io = new Server(0);
const socket = createClient(io, "/chat");

Expand All @@ -740,11 +740,11 @@ describe("socket", () => {
expect(packet.options.compress).to.be(true);
success(done, io, socket);
});
io.of("/chat").emit("woot", "hi");
s.emit("woot", "hi");
});
});

it("should disable compresion", (done) => {
it("should disable compression", (done) => {
const io = new Server(0);
const socket = createClient(io, "/chat");

Expand All @@ -753,7 +753,7 @@ describe("socket", () => {
expect(packet.options.compress).to.be(false);
success(done, io, socket);
});
io.of("/chat").compress(false).emit("woot", "hi");
s.compress(false).emit("woot", "hi");
});
});

Expand Down

0 comments on commit da2b542

Please sign in to comment.