Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

javascript - Where to hold my socket in my redux store?

Currently I am using slices and redux-thunks to create my redux store. I am fairly new to redux and I quickly realized that state is not the place to store my socket connection. The socket connection is global and is used by almost all of the components. And most importantly the socket connection needs to stay open, so I can't be making a new connection on each dispatched action.

Any advice, suggestions, or examples would be much appreciated.

question from:https://stackoverflow.com/questions/65940957/where-to-hold-my-socket-in-my-redux-store

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Websockets should typically go in a Redux middleware, where they have access to dispatch and getState:

https://redux.js.org/faq/code-structure#where-should-websockets-and-other-persistent-connections-live


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...