Fork me on GitHub
#re-frame
<
2021-08-22
>
wombawomba16:08:34

What's the recommended approach for using websockets with re-frame?

p-himik16:08:33

Not sure why there has to be a recommended approach - the two are completely orthogonal. I prefer to use it with core.async wrapping sente. I just have a go-loop at an app's entry point that fetches sente's messages and dispatches relevant events. And I have some effects that send messages via the sente's open channel.

wombawomba17:08:21

hah fair enough

wombawomba17:08:24

what's the other approach?

wombawomba17:08:21

sente seems a bit heavyweight to me, not sure if I'll need it

p-himik17:08:16

The other? :D I'd imagine there are quite a few. You can use something other than sente and/or core.async. You can react to messages with just one event or with many. You can store the current connection in a separate atom (that's what I do) or in some closure, or in the app-db itself. You can send re-frame event vectors over the wire and make the other part handle them, or you can send back and forth some specific data that has nothing to do with events. There are likely other degrees of freedom that I can't think of right now.

wombawomba17:08:27

very informative, thanks

👍 2
wombawomba17:08:14

thinking about how to piece the two together hurts my head a bit

wombawomba17:08:37

I think I'll just go with a regular JS websocket client that I put in the app db

wombawomba17:08:22

...and then spawn a re-frame event for each event I get over the websocket

p-himik17:08:54

Will be easier if you start small. Re-frame interfaces with the outside world via the world dispatching events and via re-frame doing stuff in effect handlers - that's it. That's the only constant in all the ways of using WebSockets with re-frame. So if you write something tiny that does that, you will pretty much grasp the whole concept.

wombawomba17:08:15

sounds good, thanks

wombawomba17:08:55

the tricky part is that I'll need to use websockets to keep objects I've fetched over ajax in up to date

wombawomba17:08:10

but I'm fairly confident I'll find a way to deal with that

wombawomba17:08:41

...once I've got something basic going