Skip to content

Commit

Permalink
early ack only for SESSION_ENDED
Browse files Browse the repository at this point in the history
  • Loading branch information
jibon57 committed Sep 27, 2024
1 parent 27a4d1a commit 6ed211b
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/helpers/nats/ConnectNats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,18 @@ export default class ConnectNats {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
for await (const m of sub) {
m.ack();
try {
const payload = fromBinary(NatsMsgServerToClientSchema, m.data);
if (payload.event === NatsMsgServerToClientEvents.SESSION_ENDED) {
// otherwise if connection closed then ack will not process
m.ack();
}
await this.handleSystemEvents(payload);
} catch (e) {
const err = e as NatsError;
console.error(err.message);
}
m.ack();
}
}

Expand All @@ -420,14 +424,18 @@ export default class ConnectNats {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
for await (const m of sub) {
m.ack();
try {
const payload = fromBinary(NatsMsgServerToClientSchema, m.data);
if (payload.event === NatsMsgServerToClientEvents.SESSION_ENDED) {
// otherwise if connection closed then ack will not process
m.ack();
}
await this.handleSystemEvents(payload);
} catch (e) {
const err = e as NatsError;
console.error(err.message);
}
m.ack();
}
};

Expand All @@ -443,14 +451,14 @@ export default class ConnectNats {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
for await (const m of sub) {
m.ack();
try {
const payload = fromBinary(ChatMessageSchema, m.data);
await this.handleChat.handleMsg(payload);
} catch (e) {
const err = e as NatsError;
console.error(err.message);
}
m.ack();
}
};

Expand All @@ -466,7 +474,6 @@ export default class ConnectNats {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
for await (const m of sub) {
m.ack();
try {
const payload = fromBinary(DataChannelMessageSchema, m.data);
// whiteboard data should not process by the same sender
Expand All @@ -477,6 +484,7 @@ export default class ConnectNats {
) {
// receiver specified & this user was not the receiver
// we'll not process further
m.ack();
continue;
}
await this.handleWhiteboard.handleWhiteboardMsg(payload);
Expand All @@ -485,6 +493,7 @@ export default class ConnectNats {
const err = e as NatsError;
console.error(err.message);
}
m.ack();
}
};

Expand All @@ -500,7 +509,6 @@ export default class ConnectNats {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
for await (const m of sub) {
m.ack();
try {
const payload = fromBinary(DataChannelMessageSchema, m.data);
if (
Expand All @@ -509,6 +517,7 @@ export default class ConnectNats {
) {
// receiver specified & this user was not the receiver
// we'll not process further
m.ack();
continue;
}
// fromUserId check inside handleMessage method
Expand All @@ -517,6 +526,7 @@ export default class ConnectNats {
const err = e as NatsError;
console.error(err.message);
}
m.ack();
}
};

Expand Down

0 comments on commit 6ed211b

Please sign in to comment.