I try to use socketCluster with acknowledgment in NodeJS.
I follow the code in this page: https://socketcluster.io/docs/basic-usage/
[Server] Publish to a channel and wait for acknowledgement
(async () => {
try {
// Publish data; wait for an acknowledgement from the back end broker (if it exists).
await agServer.exchange.invokePublish('foo', 'This is some more data');
} catch (error) {
// ... Handle potential error if broker does not acknowledge before timeout.
}
})();
But I don't get an acknowledgment and I don't know what is missing. I have tried uploading the socket cluster server with this option {ackTimeout: 10000}, but it didn't help. How do I know if the message was received?
To reply to your last comment @Lior. The "catch" block will be executed if the ACK has not been received. How to properly retry = I don't know. I am not a SocketCluster expert, but it seems there is no automatic mechanism to handle this. You could put a "do / while" loop around the try / catch block (by preventing infinite loop) :
let i = 0;
let callIsSuccessful = false;
do {
try {
// Publish data; wait for an acknowledgement from the back end broker (if it exists).
await agServer.exchange.invokePublish('foo', 'This is some more data');
callIsSuccessful = true;
} catch (error) {
// ... Handle potential error if broker does not acknowledge before timeout.
}
} while (i++ < MAX_RETRIES && (!callIsSuccessful))
You could also recall the function which make the publish action, by recursive way :
callingFunction(paramData, currentRetryNumber){
if(currentRetryNumber >= MAX_RETRIES){
return;
}
try {
// Publish data; wait for an acknowledgement from the back end broker (if it exists).
await agServer.exchange.invokePublish('foo', 'This is some more data');
} catch (error) {
callingFunction(paramData, ++currentRetryNumber);
// ... Handle potential error if broker does not acknowledge before timeout.
}
}
(async () => {
callingFunction(paramData, 0);
})();
Is it satisfying ? ;)