Running a socket emit in a library action causes errors since 5.8.0:
ClientClosedError: The client is closed
at Commander._RedisClient_sendCommand (/Users/tobi/dev/wpaas/node_modules/@redis/client/dist/lib/client/index.js:490:31)
at Commander.commandsExecutor (/Users/tobi/dev/wpaas/node_modules/@redis/client/dist/lib/client/index.js:188:154)
at Commander.BaseClass.<computed> [as publish] (/Users/tobi/dev/wpaas/node_modules/@redis/client/dist/lib/commander.js:8:29)
at RedisAdapter.broadcast (/Users/tobi/dev/wpaas/node_modules/@socket.io/redis-adapter/dist/index.js:473:28)
at BroadcastOperator.emit (/Users/tobi/dev/wpaas/node_modules/socket.io/dist/broadcast-operator.js:169:26)
at App.exports.emit (/Users/tobi/dev/wpaas/lib/modules/sockets.js:20:42)
at App._exec (/Users/tobi/dev/wpaas/lib/core/app.js:608:57)
at App.exec (/Users/tobi/dev/wpaas/lib/core/app.js:544:20)
at App._exec (/Users/tobi/dev/wpaas/lib/core/app.js:565:25)
at async App._exec (/Users/tobi/dev/wpaas/lib/core/app.js:575:17)
Executing the socket emit directly in an API workflow works without issues. Running it via exec or include library action causes the above error.
The library action only contains a socket emit and nothing else.
global.redisClient.duplicate() was called immediately upon evaluating if (global.redisClient) . This caused an error when the client wasn’t fully connected before invoking the duplicate() method.
Now it waits for the ‘connect’ event on the Redis client before creating the duplicate and setting up the adapter. This avoids the error by ensuring the client is fully connected before calling duplicate().
I implemented it a little different, if the client was already connected the connect event is not triggered. Instead I call connect again on the duplicate client, it returns a promise which resolves when it is connected or resolved directly when already connected.