Fork me on GitHub

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.


maybe how websockets are handled is quite similar.


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 🙂


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.


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

isker23:12:52 makes it sound like subscriptions are only for moving data from app-db into view functions


you can do other things with them?


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


(I don't know anything about chrome extensions)


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?


or do i need to start that out of band?


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


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


But, you could probably also do that


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


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


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


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


thanks for your help!