Skip to content

Commit 619507a

Browse files
authored
Clarify websockets answer.
1 parent 32344fa commit 619507a

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

docs/faq/CodeStructure.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,14 @@ Action creators are a more powerful abstraction. Creating an action often involv
107107
<a id="structure-persistent-connections"></a>
108108
### Where should websockets and other persistent connections live?
109109

110-
It is highly recommended to put any kind of persistent connection into middleware. Which should intercept needed actions and also handle any complex logic or [side effects](/faq/actions#actions-side-effects) it may produce. It is also possible to put persistent connections into the store state, but you should be aware that it may cause some [issues](/faq/organizing-state#organizing-state-non-serializable).
110+
Middleware are the right place for persistent connections like websockets in a Redux app, for several reasons:
111+
112+
- Middleware exist for the lifetime of the application
113+
- Like with the store itself, you probably only need a single instance of a given connection that the whole app can use
114+
- Middleware can see all dispatched actions and dispatch actions themselves. This means a middleware can take dispatched actions and turn those into messages sent over the websocket, and dispatch new actions when a message is received over the websocket.
115+
- A websocket connection instance isn't serializable, so [it doesn't belong in the store state itself](/faq/organizing-state#organizing-state-non-serializable)
116+
117+
There's many existing middleware for websockets and other similar connections - see the link below.
111118

112119
**Libraries**
113-
- [Middleware: Socket and Adapters](https://github.com/markerikson/redux-ecosystem-links/blob/master/middleware-sockets-adapters.md)
120+
- [Middleware: Socket and Adapters](https://github.com/markerikson/redux-ecosystem-links/blob/master/middleware-sockets-adapters.md)

0 commit comments

Comments
 (0)