Skip to content

Fix presence:subscribed not sent to joining client#8

Open
Oha-you wants to merge 1 commit into
AbdoPrDZ:mainfrom
Oha-you:fix-subscribed
Open

Fix presence:subscribed not sent to joining client#8
Oha-you wants to merge 1 commit into
AbdoPrDZ:mainfrom
Oha-you:fix-subscribed

Conversation

@Oha-you

@Oha-you Oha-you commented Mar 20, 2026

Copy link
Copy Markdown

This PR fixes the handling of the presence:subscribed event.

Currently, the event is broadcast to all clients except the one that just joined. As a result, the joining client never receives the initial member list and shows 0 users.

This change uses emit instead, ensuring the event is sent to the joining socket so it can properly initialize its state.

This partially reverts the behavior introduced in: 116247d

Note: I had to talk to ChatGPT to understand and fix this issue. This was the response:

ChatGPT answer 🤖

The problem is subtle:

You are using broadcast().

broadcast() sends to everyone EXCEPT the socket specified in socket:.

That means:

  • The joining socket does NOT receive presence:subscribed
  • Only other clients do

That explains everything.

🧠 Why This Matches Your Symptoms

On first page load:

  • You join
  • onSubscribed() runs
  • broadcast() excludes your socket
  • You NEVER receive presence:subscribed
  • .here() never fires

When another browser refreshes:

  • That second socket joins
  • presence:joining is broadcast
  • YOU receive that
  • Your users list updates

So it looks like .here() started working - but it never actually did.

🏆 The Correct Patch

You must send presence:subscribed directly to the joining socket.

Replace:

this.options.echoServer.broadcast(channel, {
    socket: socket.id,
    event: "presence:subscribed",
    data: members,
});

With:

socket.emit("presence:subscribed", channel, members);

So your patched method becomes:

PresenceChannel.prototype.onSubscribed = function (socket, channel, members) {
    socket.emit("presence:subscribed", channel, members);
};

But you probably have a better understanding of this module since you've changed it from emit to broadcast previously.
I'm already using a patched version myself, so feel free to decline this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant