Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publishing to rooms isn't working after connection recovery with uWebSockets #4810

Open
ABE-Mark45 opened this issue Aug 26, 2023 · 0 comments
Labels
to triage Waiting to be triaged by a member of the team

Comments

@ABE-Mark45
Copy link

Describe the bug
I am creating a simple pub-sub system in which I have

  • A socket.io server integrated with uWebSockets and reconnection recovery is enabled
  • A publisher emitting messages periodically to a certain room
  • A subscriber listening to messages sent to this room and closing the connection deliberately periodically

The expected behavior is after reconnection the subscriber should receive the buffered messages at reconnection period and continue receiving newer messages. However, what really happens is that the subscriber only receive the buffered messages and doesn't receive any newer ones. I am using socket.io.engine.close() to deliberately close the connection (same happens if you just close the connection from Chrome Dev Tools. The current weird behavior is whenever it is called, a surge of buffered messages is processed by the client all at once. So it seems that room messages are well-received by the client, but they events are not passed to callbacks for some reason. The sever is working properly if an httpServer is used instead.

To Reproduce
Socket.IO server version: 4.7.2

Server

const { Server } = require("socket.io");
const { App } = require("uWebSockets.js");

const app = new App();
const io = new Server({
  connectionStateRecovery: {
    maxDisconnectionDuration: 2 * 60 * 1000,
    skipMiddlewares: true,
  },
  cors: {
    origin: "*",
  },
});

io.attachApp(app);

io.on("connection", (socket) => {
  if (socket.recovered) {
    console.log("A user recovered", socket.id);
  } else {
    console.log("A user connected");

    socket.on("subscribe", (channel) => {
      socket.join(channel);
      console.log(`User subscribed to channel: ${channel}`);
    });

    socket.on("publish", (data) => {
      io.to(data.channel).emit("message", data.message);
    });
  }
});

app.listen(3000, () => {
  console.log("server listening on port 3000");
});

Socket.IO client version: 4.7.2

Publisher

const io = require("socket.io-client");
const socket = io("http://localhost:3000");

socket.on("connect", async () => {
  socket.emit("subscribe", "my-channel");

  let count = 0;
  setInterval(() => {
    socket.emit("publish", {
      channel: "my-channel",
      message: {
        count: count++,
      },
    });
    console.log("published: ", count);
  }, 1000);
});

Subscriber

const io = require("socket.io-client");
const socket = io("http://localhost:3000");

socket.on("connect", async () => {
  if (socket.recovered) {
    console.log("Reconnected to server", socket.rooms);
  } else {
    socket.emit("subscribe", 'my-channel');
  }
});

socket.on("message", (data) => {
  const { count } = data;
  console.log(count);
});

setInterval(() => {
  socket.io.engine?.close();
}, 10000);

Expected behavior
The expected behavior is after reconnection the subscriber should receive the buffered messages at reconnection period and continue receiving newer messages.

Platform:

  • Device: [i7-8750H CPU, 16 GB RAM]
  • OS: [Windows 10]
@ABE-Mark45 ABE-Mark45 added the to triage Waiting to be triaged by a member of the team label Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
to triage Waiting to be triaged by a member of the team
Projects
None yet
Development

No branches or pull requests

1 participant