Fork me on GitHub
#re-frame
<
2018-12-02
>
isker22:12:09

can anyone point me to example code that starts some kind of bidirectional channel? i am working on a chrome extension where the UI will use re-frame; it connects to the "backend" of the extension via a channel in the chrome API. the frontend and backend asynchronously message each other with the channel. i want to receive messages on the re-frame side of this channel and dispatch events with their data, and also handle some events by sending messages to the backend. but i am not sure when/how to create and store the actual "socket" that you send messages over.

isker22:12:13

maybe how websockets are handled is quite similar.

isker22:12:30

maybe i should just connect and store the channel in a top-level atom totally outside of re-frame but that feels kind of lame 🙂

lilactown22:12:42

I haven't done this before but I would guess that you could add a subscription to the socket that would dispatch a message to re-frame.

lilactown22:12:08

Likewise, register an effect that sends a message to the socket that re-frame event-handlers can use

isker23:12:52

https://github.com/Day8/re-frame/blob/master/docs/SubscriptionInfographic.md makes it sound like subscriptions are only for moving data from app-db into view functions

isker23:12:00

you can do other things with them?

lilactown23:12:14

Sorry, not a re-frame subscription. However you would "subscribe" to messages on the "socket"

lilactown23:12:27

(I don't know anything about chrome extensions)

isker23:12:01

ah okay 🙂. the question is can i run that subscription from within re-frame? i.e. on initialization can i dispatch an :init event that, among other things, returns an effect that starts this subscription? can you have effect handlers that do not ever return?

isker23:12:19

or do i need to start that out of band?

lilactown23:12:58

Hmm I would just do it out of band, unless there was some sort of timing component to the subscription

isker23:12:11

(under the hood this thing is a core.async channel, so it's doing that kind of subscription with a go-loop)

lilactown23:12:17

But, you could probably also do that

lilactown23:12:14

Like if you need to wait for the user to do something before starting the go-loop

isker23:12:43

nope, not really. it's just a static external resource.

isker23:12:59

i will probably do it as you recommend then :thumbsup:

lilactown23:12:06

But it all seems like a lot of ceremony. I would just start the go-loop and rf/dispatch on message receive

isker23:12:34

thanks for your help!