isomorphic-ws | Isomorphic implementation of WebSocket ( https | Websocket library
kandi X-RAY | isomorphic-ws Summary
kandi X-RAY | isomorphic-ws Summary
Isomorphic implementation of WebSocket (
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Find a new module .
isomorphic-ws Key Features
isomorphic-ws Examples and Code Snippets
Community Discussions
Trending Discussions on isomorphic-ws
QUESTION
I'm building a react app with recoiljs as my state manager and I need to fetch data from a web socket and update the state accordingly. The reason I'm not using a regular array is that I need to share state across my components.
To elaborate, the component has a WebSocket connection and will have data streaming into it. It then needs to update an array by pushing the received data. The problem is due to the component lifecycle or something, the state gets written incorrectly and this is what I think happens
- Response 1 gets received and setState([...state, data]) is called
- Response 2 then gets received and setState([...state, data]) is called
- The state is actually set by Response 1
- The state is set by Response 2
The problem is when response 2 comes in and setState is called, it has an old version of the state that doesn't include Response 1.
I can't seem to figure out how to fix this. Here is the stripped-down code.
...ANSWER
Answered 2021-Jun-07 at 20:31As Jayce444 said, the way to solve this is instead of using setStatus
with an already computed value, [...status, res.data]
, I should instead pass in a lambda statement that is called when the state is updated.
Instead of:
setStatus([..status, res.data])
Use: setStatus(prevStatus => [...prevStatus, res.data])
QUESTION
I am trying to use websockets to connect to Kraken websocket API. I create a new instance and setup listeners on intitial render. I am trying to make buttons to subscribe and close but the buttons are not working.
I am using this websocket library.
...ANSWER
Answered 2021-Jan-04 at 19:29Right now, you're creating a new socket every time the component re-renders. The effect callback references the first socket created (on mount), but the wsClose
and wsSub
do not (they reference the socket created in the immediately previous render).
Put the socket into a state or a ref:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install isomorphic-ws
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page