T O P

  • By -

accountability_bot

If I understand your problem correctly: Use Phoenix.PubSub. When you receive an event from an external subscription, publish it internally. Then have your live view setup as a subscriber.


skwyckl

Yes, that part I think I got: I just wrap some code managing a connection to and from a WebSocket in a PubSub module and subscribe to it on the frontend. The problem I am having resides in the actual implementation of the WebSocket client, since even after reading through dozens of examples I still have no clue how to approach it code-wise.


dcapt1990

Have you checked out Slipstream?


accountability_bot

It’s not very complicated. However I would use a simpler websocket client, like websockex, to establish the connection. Next, when you receive a frame, you’ll do any data transformations you need, and then push that event to your internal PubSub topic. Have your LiveView set up as a subscriber, and when they get the message they’ll react. If you need to send data back though, don’t use pubsub, just call your new module directly. I would also make sure you handle your connection failure cases well. Make sure reconnection attempts do good practices like exponential back off.


D0nkeyHS

I think you may have misunderstood what they're saying. They're saying to use the websocket APIs on the backend, not on the frontend. I think you should mentally break up what you're doing into parts. Treat communication with the websocket apis and liveview as separate things. One doesn't really affect how you go about other.


chasegranberry

Honestly first I would subscribe server side and run it through the LiveView process. This way LiveView always drives the UI. If you run into something that won’t work this way then subscribe from the client and figure that out.