Skip to content

Commit 4e64123

Browse files
feat: expose current offset to allow deduplication
Related: socketio/socket.io-client@655dce9
1 parent 115a981 commit 4e64123

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/socket.ts

+17
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,22 @@ export class Socket<
217217
* });
218218
*/
219219
public connected: boolean = false;
220+
/**
221+
* The identifier of the current packet. Multiple retries of the same packet will have the same identifier, in order
222+
* to allow deduplication (only process a packet once).
223+
*
224+
* @example
225+
* io.on("connection", (socket) => {
226+
* socket.on("my-event", async (payload, callback) => {
227+
* const offset = socket.currentOffset;
228+
*
229+
* await insertInDatabase(payload, offset);
230+
*
231+
* callback();
232+
* })
233+
* });
234+
*/
235+
public currentOffset: string;
220236

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

657674
if (this._anyListeners && this._anyListeners.length) {

test/socket.ts

+24
Original file line numberDiff line numberDiff line change
@@ -1105,4 +1105,28 @@ describe("socket", () => {
11051105
socket3.on("disconnect", partialDone);
11061106
});
11071107
});
1108+
1109+
// TODO: enable once a new version of the socket.io-client package is released
1110+
// it("should retry with the same packet ID", (done) => {
1111+
// const io = new Server(0);
1112+
// let counter = 0;
1113+
//
1114+
// io.on("connection", (socket) => {
1115+
// socket.on("my-event", (cb) => {
1116+
// expect(socket.currentOffset).to.eql(socket.id + "-0");
1117+
// if (++counter === 3) {
1118+
// cb();
1119+
//
1120+
// success(done, io, clientSocket);
1121+
// }
1122+
// });
1123+
// });
1124+
//
1125+
// const clientSocket = createClient(io, "/", {
1126+
// retries: 10,
1127+
// ackTimeout: 20,
1128+
// });
1129+
//
1130+
// clientSocket.emit("my-event");
1131+
// });
11081132
});

0 commit comments

Comments
 (0)