So this is what I’m trying to achieve:
One part of my javascript client side code joins a topic "data:data"
with a payload of data to sync to the backend.
def join("data:data", %{"query" => q} = params, socket) when is_binary(q) do
send(self(), {:sync_data, params})
{:ok, socket}
end
def handle_info({:sync_data, params}, socket) do
response = response(params)
broadcast(socket, "data-synced", response)
{:noreply, socket}
end
This part of the client side code subscribes to the "data-synced"
event and does something with the response. So far this part is working:
this.dataChannel = this.socket.channel("data:data", params);
this.dataChannel.on("data-synced", this.dataSyncedCb);
Now other parts of the code using the same socket (I’m using Phoenix.js) will join another topic "data:client:" <> id
and also subscribe to the "data-synced"
event and do different things with the response.
def join("data:client:" <> _id, _params, socket), do: {:ok, socket}
this.dataSyncChannel = args.socket.channel("data:client:289", {});
this.dataSyncChannel.on("data-synced", this.dataClientSyncedCb);
But these other parts of the code are not receiving the data-synced
event.
Please note that these are all the same clients but they all have different responsibilities for the event response.
So I’m thinking may be Phoenix.Channel.broadcast/3
is not the way to go.