Fork me on GitHub
#re-frame
<
2017-12-30
>
jmglov15:12:25

I'm working on a ClojureScript app that needs to react to messages that it receives on an AWS SQS queue. What's the best practice for an endless receive loop in re-frame? I also need to handle user input, so it can't be blocking.

souenzzo15:12:49

I think that you will use something like this @jmglov

(doto (new SQSConnection "")
  (.addHandler "on-messsage" (fn [m] (rf/dispatch [:new-sqs-message m]))))

jmglov15:12:16

Interesting. Is that using cljsjs/aws-sdk-js?

souenzzo15:12:18

No. This is how I imagine the interface is

jmglov15:12:48

Ah... no, it's not like that.

jmglov15:12:58

I basically need to call (,receiveMessage sqs ...) in an endless loop.

jmglov15:12:15

There's no subscription with a handler.

jmglov15:12:37

I'm not sure where to put my endless loop and make it non-blocking.

souenzzo15:12:39

It's a blocking call?

jmglov15:12:56

I'm sure core.async is my friend, but I'm not sure how and where to apply it.

jmglov15:12:21

The call itself is quick, so I don't mind if it blocks for a couple milliseconds.

jmglov15:12:43

But I need to poll for new messages once per second or so.

souenzzo15:12:01

you can make it with timers*, every 10 seconds, call it and get new messages and dispatch to re-frame. *`js/setTimeout`

jmglov15:12:56

Yup, I'm going that route for now, with goog.Timer.

jmglov15:12:16

Seems yucky, but it should get the job done until I find a better way. 🙂

mikethompson21:12:33

@jmglov do you have to turn this SQS polling "on" and "off" depending on application state? Or does it have to happen for the entire life of the application?

mikethompson21:12:28

If the former, then you'll have to create an "effect" to do the turning "on" and "off". This FAQ may help: https://github.com/Day8/re-frame/blob/master/docs/FAQs/PollADatabaseEvery60.md (although you may well want a variation on this for your usecase) If the later, then your job is obviously simpler: just set up the timer when the app starts up, and leave it plugging away in the background, dispatching when something interesting happens.