Skip to content

How to implement external subscription management? Websocket, timeouts, interval, etc, missing onMount #635

Answered by JoviDeCroock
mxck asked this question in Q&A
Discussion options

You must be logged in to vote

Making a subscription is basically a side-effect so using something useSignalEffect in signals or useEffect(()=>,[]) in hooks would do that.

The effect will run onMount and based on dependencies re-run again, in your case you don't want it to rerun so signals would be accessed with untracked or peek.

only mounting

useSignalEffect(() => {
  const unsubsribe = subscribeToSomeWebsocket() // will be called when the component mounts
  return unsubscribe // will be called when the component unmounts
})

with updates

useSignalEffect(() => {
  const unsubsribe = subscribeToSomeWebsocket(id.value) // will be called when the component mounts and  id signal updates
  return unsubscribe // will be cal…

Replies: 0 comments 6 replies

Comment options

You must be logged in to vote
5 replies
@mxck
Comment options

@JoviDeCroock
Comment options

@mxck
Comment options

@JoviDeCroock
Comment options

@mxck
Comment options

Answer selected by JoviDeCroock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants