Skip to content

Commit

Permalink
Fix message wrapping for Redis distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
oklemenz2 committed Jan 31, 2024
1 parent 99beb9e commit a64491a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Version 0.6.0 - 2024-02-xx
## Version 0.5.1 - 2024-01-31

### Fixed

- tbd
- Fix message wrapping for Redis distribution (`kind: ws`)

## Version 0.5.0 - 2024-01-29

Expand Down
5 changes: 3 additions & 2 deletions src/socket/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ class SocketWSServer extends SocketServer {
}

async broadcast({ service, event, data, tenant, contexts, socket, remote }) {
if (!data) {
const eventMessage = !data;
if (eventMessage) {
const message = JSON.parse(event);
data = message.data;
tenant = message.tenant;
Expand All @@ -150,7 +151,7 @@ class SocketWSServer extends SocketServer {
}
});
if (clients.length > 0 || remote) {
const message = !data ? event : JSON.stringify({ event, data, tenant, contexts });
const message = eventMessage ? event : JSON.stringify({ event, data, tenant, contexts });
for (const client of clients) {
await client.send(message);
}
Expand Down
3 changes: 2 additions & 1 deletion test/_env/util/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ async function emitEvent(socket, event, data) {
});
}

async function waitForEvent(socket, event) {
async function waitForEvent(socket, event, cb) {
return new Promise((resolve) => {
socket.on("message", (message) => {
const payload = JSON.parse(message);
if (payload.event === event) {
resolve(payload.data);
cb && cb(payload);
}
});
});
Expand Down
25 changes: 24 additions & 1 deletion test/redis_ws.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ describe("Redis", () => {
});

test("Redis adapter", async () => {
const waitResultPromise = waitForEvent(socket, "received");
const messages = [];
const waitResultPromise = waitForEvent(socket, "received", (message) => {
messages.push(message);
});
const waitNoResultPromise = waitForNoEvent(socketOtherTenant, "received");
await emitEvent(socket, "message", { text: "test" });
const waitResult = await waitResultPromise;
Expand All @@ -53,5 +56,25 @@ describe("Redis", () => {
"websocket/chat",
`{"event":"received","data":{"text":"test","user":"alice"},"tenant":"t1"}`,
);

// Duplicated because Redis mock publishes to same client (not done for real Redis)
expect(messages).toEqual([
{
data: {
text: "test",
user: "alice",
},
event: "received",
tenant: "t1",
},
{
data: {
text: "test",
user: "alice",
},
event: "received",
tenant: "t1",
},
]);
});
});

0 comments on commit a64491a

Please sign in to comment.