Comments (8)
It's a bit difficult to say without looking at more specific examples. But let me try to make some assumptions and suggest something, and then you can tell me what does not match.
I have the same case at work, where we start and stop websockets on certain URLs. The code is not that much shorter in kee-frame, but in my experience it's much easier to follow the logic.
- We enter the url
/your/page/that/needs/a/socket
- The controller matches that url, triggers
[:my-socket/start]
- The
[:my-socket/start]
event also modifies app-db to indicate that the page is in a loading stage - When your socket loading process is done, app-db is updated and page views nicely
- When user navigates away from the url, controller triggers
[:my-socket/stop]
Nothing stops you from using several different controllers for the same URL. I discovered this trick early on, it's very useful! See https://github.com/ingesolvoll/kee-frame-sample/blob/master/src/cljs/kee_frame_sample/controller/live.cljs, where I used this trick to trigger an event both immediately and every 10 seconds.
from kee-frame.
(reg-controller ::socket-controller
{:params (fn [{:keys [handler route-params]}]
(when (= :my-socket-page handler)
true)) ;; Or some other value that should be sent to the start event
:start [::start-the-socket]
:stop [::stop-the-socket]})
from kee-frame.
Thanks for the nice hint. Even though I need a websocket for all urls I will use the suggested logic.
Besides that I still have the problem of how to delay the request of further data in the controller until the websocket is established. Currently I am using some kind of queuing to accomplish this.
- Open the url /your/page/that/needs/a/socket/and/data/XYZ.
- A controller triggers the event [:start-the-socket] and another controller triggers [:queue-data-to-request "XYZ"]. (The order of these events is out of my control.)
- The handler for :queue-data-to-request appends some {:queue-data-to-request "XYZ"} to app-db.
- When the websocket is established an event [:websocket-established] is triggered.
- The handler for :websocket-established dispatches events that request the data from :queue-data-to-request.
I am not that experienced in developing a SPA but for me it seems a lot of code for a "common problem". Moreover it suffers of a (maybe unrealistic) race-condition that occurs when the websocket is established before the handler for :queue-data-to-request was executed.
Do you know any logic/pattern to avoid the drawbacks of my current solution?
from kee-frame.
Actually we have pretty much identical problems on our end. You depend on a socket for sending and receiving messages. Both initial messages and continuous messages. The initial message is sent only once, so you can't drop that one. And you don't have control over timing, so initial message must be queued somehow until the socket is ready.
I'm not sure if this is something kee-frame is supposed to solve. But if there is a natural fit, I would love to hear any ideas. We have tried a few things, in addition to your procedure. One promising lead is to put a core.async buffer channel in front of the websocket, so you send message to the buffer channel. Then there is logic to pipe messages from the buffer channel to the websocket when it's available.
from kee-frame.
Thanks a lot for your reply. This gives me confidence that I do not solve the problem totally inappropriate.
From my point of view I would appreciate the integration of an optional buffer channel into kee-frame. Moreover you can provide effect-handlers for [:add-event-to-queue myevent] and :dispatch-all-queued-events (or/and :dispatch-last-queued-event).
Well, obviously the decision is up to you whether this in the scope of kee-frame or not.
from kee-frame.
For now I'm thinking it's outside the scope.
It was definitely very useful to hear about your case though. As I said we are working on the same thing, and we're not happy how it looks either. Will keep working on it, and it might become a blog post or something else in the future. Thanks for posting the issue, will close it now.
from kee-frame.
I look forward to any blog post that might come out regarding this. I am also interested in adding web sockets to my kee-frame application.
from kee-frame.
Got some ideas around this, will use this issue to track them
from kee-frame.
Related Issues (20)
- ::fsm/on-enter subject to race condition HOT 3
- `switch-route` doesn't seem to honour the `nil` case HOT 4
- Unable to set the order of the controllers HOT 1
- Consider allowing specifying actions on DB spec check failure HOT 1
- Consider splitting statecharts-re-frame integration into a separate library HOT 1
- Use speced.def for some core APIs
- "Scroll behavior on navigation" section of the readme seems to be outdated. HOT 2
- Hash Routing on subdirectory not working
- Feature Request: On navigate fx option in `navigate-to` effect.
- make interop/render-root to support React 18?
- provide a way to opt out of spec dependencies
- API for overriding certain interceptors
- Controller 2's :start is called before Controller 1's :stop HOT 4
- Use a logging library for debug logging HOT 1
- Using path-params in navigate-to fx HOT 5
- Question regarding reg-controller HOT 7
- Upgrade to re-frame 1.0
- FSM: stop/start behaviour clarification HOT 3
- How does the section about logging relate to the source code?
- Upgrade com.taoensso/timbre dep to avoid compilation warnings HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kee-frame.