Socket action not working when redis is enabled

Wappler 7.7.9
NodeJS

Spend a lot of time wondering why emit stopped working on a project when in another one it works with no issues.
The problem is: when redis is enabled direct message and emit doesn't work.

So:
I have this:

<dmx-socket id="socket1" dmx-on:connect="socket1.emit('mensaje',{texto: 123})"></dmx-socket>

Which basically it joins to a room:

That works:

And emit sends a message to that room:


Now the issue,
When redis disabled:


When redis enabled:

You can see the issue in the video:

If it helps, all good for me, nothing urgent:

Commenting this on setup/socket.js (I know it's not ideal) will work as I'm using a single server, but using redis for queues so need it enabled:

if (global.redisClient) {
        const { createAdapter } = require('@socket.io/redis-streams-adapter');
        io.adapter(createAdapter(global.redisClient));
    }

Hi Francisco,

In my testing, socket emits appear to work correctly when Redis is enabled.

Is the issue you're seeing only happening when sockets join rooms and Redis is enabled? I don’t use rooms in my setup, but understanding this would help me determine whether I need to run additional tests on socket emits in my own projects (v7.7.9).

Hello @guptast thanks for the reply, actually emit doesn’t work at all when redis is enabled, with socketid inside a room or not
Disabling redis and all working again

Not using docker if it helps

Hi Francisco,

I have tested the socket emit actions when redis is enabled, and these actions are working correctly. It seems to be an issue with the socket joining a room when redis is enabled.

Emit step in the API:

Dev console log:

Code on the content page:

<dmx-socket id="socket_emit_refresh" dmx-on:message.emit_user_id_budget="run({condition:{outputType:'boolean',if:`$event.user_id==sc_logged_in_user.data.q_logged_in_user.u_id`,then:{steps:{run:{name:'budget_refresh',outputType:'text',action:`sc_budgets.load({})`}}}}})"></dmx-socket>

Thanks again for staying on this one..

I made a test with no room and emit still not working..

Are you using docker?

Yes, I'm using Docker.

Redis is created as a separate service within each Docker container. Not sure how Redis server is run in a non-Docker setup.

Thanks @guptast will do some more research

@patrick sorry for the mention, but is it possible that redis isn't ready when the adapter is mounted?

I believe the socket redis adapter will connect itself when it is not connected, so it doesn't need to be ready.

We use the redis streams adapter with ioredis client. The examples simply pass the client without connecting first like with the standard redis client.

Redis Streams adapter | Socket.IO

Yes, you're correct, also made some console log and can confirm it has nothing to do with time issues..

Will try to create a repro outside Wappler to see if the issue relies purely on redis-streams-adapter, which also has a similar recent bug reported, but closed due to inactivity..

Can't recreate the issue using:

    "@socket.io/redis-streams-adapter": "^0.3.1",
    "express": "^5.2.1",
    "ioredis": "^5.10.1",
    "socket.io": "^4.8.3"
const redis = new Redis("redis://localhost:6379", {
});

const io = new Server(server, {
  cors: { origin: "*" },
  adapter: createAdapter(redis) 
});
io.on("connection", (socket) => {
  console.log("socket conectado:", socket.id);

  setInterval(() => {
    console.log("emit desde server (redis)");

    socket.emit("server_msg", {
      msg: "hola desde server con redis",
      time: Date.now()
    });

  }, 3000);

image

But it seems the issue appears when using io.to(socket.id).emit and
adapter: createAdapter(redis) instead of socket.emit

I don't know if I'm searching in the right place, but I have the feeling something is not working here..

Well, it seems that my redis server version was too old, don't know why I was using a version where streams were not implemented yet (that old)

That makes sense on why redis-adapter works since they don't use streams like redis-streams-adapter does..

Thanks :slight_smile:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.