#How to use Phoenix PubSub

4 messages · Page 1 of 1 (latest)

opal yoke
#

I want users to subsribe to zones. And based off the zone receive events.

In this example I have shout. But my client receives the shout message multiple times.

What am I doing wrong?

def join("world:lobby", _message, socket) do
PubSub.subscribe(Rotb.PubSub, "zone_1")

{:ok, socket}

end

def handle_info(%{message: message}, socket) do
broadcast(socket, "shout", %{"socket_id" => socket.id, "message" => message})

{:noreply, socket}

end

def handle_in("shout", payload, socket) do
PubSub.broadcast(Rotb.PubSub, "zone_1", %{
message: payload["message"]
})

{:noreply, socket}

end

opal yoke
#

Broadcast is getting executed multiple times for a given PID. No clue why.

sour axle
#

I think PubSub.broadcast and broadcast are basically doing the same thing, so since you're calling them both, that's why you're getting the message twice

#

phoenix channels use PubSub under the hood and when using channels, the subscribing and broadcasting are sorta handled for you. Adding manual usage of PubSub on top of that I think is confusing...