phoenix-liveview-counter-tutorial icon indicating copy to clipboard operation
phoenix-liveview-counter-tutorial copied to clipboard

Which pubsub?

Open ndrean opened this issue 3 years ago • 1 comments

This is just an open question. I believe that Phoenix ships three ways for real-time "pubsubing": Channels, Phoenix.PubSub and MyAppWeb.Endpoint. I imagine there are all abstractions above the same Phoenix.PubSub, but any guidance on how/why you would pick up one?

ndrean avatar Aug 24 '22 11:08 ndrean

In the example dwyl/phoenix-liveview-chat-example, you use "pubsub". In the example Phoenix-liveview-counter-tutorial, you use "endpoint". In the example Phoenix-chat-example, you use "channel".

Between "pubsub" and "endpoint" and "channel", I only see a difference in the formulation:

  • pubsub: broadcast(name, topic, {event, payload})
  • endpoint: broadcast(topic, event, payload)
  • channel: broadcast!(socket, event, payload) The main difference is that the first and second handle the message with handle_info whilst the third with handle_in. You can also push... I also need to wirite %Phoenix.Socket.Broadcast{...} to receive using the "endpoint", whilst not with the "pubsub". Then I understand that what makes it "real-time" (in the browser) is that when you mutate the assigns, you render.

In the docs, I found a kind of explanation with the "context". It is not cristal clear.

Another useful answer

ndrean avatar Aug 29 '22 12:08 ndrean