Interactive frontend development
Urmas Talimaa
SaleMove Inc
Litmus test for libraries/frameworks
React/Redux
React/Redux
React/Redux
(props) => pseudoHtml
(action, previousState) => newState
Why all this functional programming nonsense?
Can’t I just hack things together?
Why all this functional programming nonsense?
In this and next seminars we’ll see how our past design choices can make magic happen.
Middleware
http://redux.js.org/docs/advanced/Middleware.html:
Redux middleware is
...a third-party extension point between dispatching an action, and the moment it reaches the reducer
Middleware
Well suited for mapping updates from a stateful source to actions.
Communication with remote servers
Long polling?
Websockets
A websocket can also send binary messages
Websockets
Websockets (and web requests) are an epitome of mutable state.
You never know what and when is coming from them or what the responses are (if responses arrive at all).
Therefore it’s necessary to put them under tight supervision and not leak them all over our beautiful application code
Testing WebSockets
This make testing WebSockets inconvenient.
There are libraries which provide mock implementations of WebSocket global (https://github.com/thoov/mock-socket)
Testing WebSockets
Luckily we can put the WebSocket connection to a middleware and just map received messages to action creators
This encapsulates all WebSocket related logic, which itself is going to be trivial.
Encapsulating WebSockets using middleware
// Using WebSocket abstraction from basic websocket example�export default (store) => (next) => {� let connection = null;� return (action) => {� if (action.type === CONNECT) {� connection = connectToWebSocket({� onOpen: () => store.dispatch(...),� onClose: ({reason}) => store.dispatch(...),� parameters: {...},� onMessage: ({eventName, payload}) => store.dispatch(...)� });� } else if (action.type === DISCONNECT) {� connection.close();� }� return next(action);
};
Distributed systems disclaimer
This is not a course on distributed systems, but...
My advisor on distributed systems
Distributed systems
From the moment a remote Server is introduced, a distributed system is created
In a distributed system it is important to understand
Distributed systems
It is very easy to create an inconsistent system.
Difficulting in retaining consistency increases when more moving parts are added to a system (e.g multiple processes for load balancing).
Distributed systems
Before using any technology that can be connected to
try to read and understand the message delivery and/or consistency guarantees.
WebSocket message delivery guarantees
Obviously not all messages are guaranteed to arrive as connections can be closed.
Game Lobby discussion
Let’s say we add a new feature to the Game Lobby homework application
All players
Detect potential problems in such a design.
Bonus points available for each distinct issue with a coherent explanation.
Object updates with WebSockets
Word of caution from personal experience
When pushing updates about large objects (e.g collections) it can sometimes be a good idea to include a partial update instead of the whole object in the message
Object updates with WebSockets
Instead of
[1,2,3]
[1,2,3,4]
[2,3,4]
Object updates with WebSockets
Server sends Initial: [1,2,3] => Client now has [1,2,3]
Server sends ADD 4 => Client now has [1,2,3,4]
Server sends REMOVE 1 => Client now has [2,3,4]
Object updates with WebSockets
As message order is guaranteed, such updates can be used to maintain an object consistently
Object updates with WebSockets
As message order is guaranteed, such updates can be used to maintain an object consistently
UNLESS
If anything fails, the maintained object is inconsistent until user refreshes
Object updates with WebSockets
The message order guarantee is only for the single WebSocket connection.
When building any protocol on top of the WebSocket connection this needs to be taken into account.
Try to use something that is battle-tested instead of homebrewing complex protocols on top of the very low level WebSocket API.
Or just send whole objects if you can...
WebSocket request response?
WebSocket is a low level protocol
It does not have request - response semantics
It barely has any semantics at all.
Can we build request-response on top of WebSockets?
WebSocket request response?
If you need it, don’t try to build it yourself
Homework
Homework
Final tips