Om nom nom nom
Talk given at Clojure/conj 2014 in Washington DC
Video available here: https://www.youtube.com/watch?v=4-oyZpLRQ20
Have you ever needed an easily customisable dashboard? Or needed to visualise data in a browser but was overwhelmed by d3.js? This talk will cover basics of React and Om, some data visualisation libraries and techniques, ways to handle live data and combining all that into an easily customisable dashboard. Expect demos, code and maybe, just maybe, om nom nom nom cookies.
1. Om Nom Nom Nom
Anna Pawlicka
Data Engineer
@AnnaPawlicka
2. Dashboard
• Communication tool
• See progress and compare values
• Show danger and success
• Share results
• Scan quickly
3. Why build your own?
• We can never make a good dashboard that
everyone will like
• Backend and front-end written in Clojure
• Clojurians like to build out of small components
• Mix and match & reuse
• It’s a good UI programming exercise
11. Facebook’s React
• Solves complex UI rendering
• Declarative framework
• One way data binding
• Maintains virtual DOM
• Diffs between previous and next renders of a UI
• Less code
• Shorter time to update
12. Om
• Entire state of the UI in a single piece of data
• Immutable data structures = Reference equality check
• No need to worry about optimisation
• Snapshot table & free undo
13. Global state
• Application state
• Global state stored in an atom
• Accessed and modified through cursors:
(def app-state (atom {:films {:data [{:v 1} {:v 2}]}}))
(-> cursor :films :data first)
:value {:v 1}
:path [:films :data 0]
:state #<Atom: {:films ..}>
Cursor
• Updated using:
(o m/update! cursor [:films :data] data)
(om/transact! cursor [:films :data] #(conj % data))
14. Local & shared state
• Local state
• Local to single component
• Best for transient state, flags
• Updated by using:
• Shared state
• Accessible by the entire tree
• Does not trigger renders
(om/set-state! owner :value value)
15. Components <-> Widgets
• D3.js
• Data bound to DOM
• Interactive - transformations driven by data
• Huge community
• Higher level libraries available
• Dimple.js
• Higher level library powered by d3
• Interactive
• Simple API
• Access to underlying D3 functions
17. How fresh is fresh data?
• You could refresh for new data
• You could schedule to pull data at some intervals
• Or you could use:
• Long polling / HTTP streaming
• WebSockets
18. WebSockets / Long polling
• Http-kit:
• Clojure HTTP server
• Great out-of-the box support for WebSockets
• HTTP streaming / Long polling
• Sente:
• Clojure + ClojureScript + core.async + ajax + WebSockets
• Simple API
• Client & server
• There are also: Timbre, Jetty 7 websockets async
32. That’s all folks!
• Try out all the amazing libraries out there
• Don’t be afraid of JavaScript interop
• Use Om with data visualisation libraries - it’s easy
• Share your components
• Share your tips and tricks
• Don’t create 3D pie charts