Skip to content

Commit

Permalink
feat: expose current offset to allow deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Feb 4, 2023
1 parent 115a981 commit 4e64123
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ export class Socket<
* });
*/
public connected: boolean = false;
/**
* The identifier of the current packet. Multiple retries of the same packet will have the same identifier, in order
* to allow deduplication (only process a packet once).
*
* @example
* io.on("connection", (socket) => {
* socket.on("my-event", async (payload, callback) => {
* const offset = socket.currentOffset;
*
* await insertInDatabase(payload, offset);
*
* callback();
* })
* });
*/
public currentOffset: string;

/**
* The session ID, which must not be shared (unlike {@link id}).
Expand Down Expand Up @@ -652,6 +668,7 @@ export class Socket<
if (null != packet.id) {
debug("attaching ack callback to event");
args.push(this.ack(packet.id));
this.currentOffset = `${this.id}-${packet.id}`;
}

if (this._anyListeners && this._anyListeners.length) {
Expand Down
24 changes: 24 additions & 0 deletions test/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,4 +1105,28 @@ describe("socket", () => {
socket3.on("disconnect", partialDone);
});
});

// TODO: enable once a new version of the socket.io-client package is released
// it("should retry with the same packet ID", (done) => {
// const io = new Server(0);
// let counter = 0;
//
// io.on("connection", (socket) => {
// socket.on("my-event", (cb) => {
// expect(socket.currentOffset).to.eql(socket.id + "-0");
// if (++counter === 3) {
// cb();
//
// success(done, io, clientSocket);
// }
// });
// });
//
// const clientSocket = createClient(io, "/", {
// retries: 10,
// ackTimeout: 20,
// });
//
// clientSocket.emit("my-event");
// });
});

0 comments on commit 4e64123

Please sign in to comment.