Skip to main content
Open a live WebSocket to the ZyFAI risk engine and get a callback the moment something changes: a stablecoin drifting off peg, a pool bleeding liquidity, or a new collateral type showing up in a vault. No wallet, no polling loop — just handlers.
Like simulateBestPositions, this only needs your API key. It’s a good fit for dashboards, alerting, and monitoring that needs to run independently of any one user’s session.

Signature

Parameters

Returns

An unsubscribe function.
subscribeToEvents opens a real WebSocket connection. Call the function it returns as soon as you no longer need the stream — on component unmount, on page leave, whenever the caller goes away — or you’ll leak the connection.

Event types

onDepeg

onLiquidityDrop

onNewCollateralDetected

Good to know

A few behaviors worth knowing before you build against this, confirmed by connecting to the live socket directly:
  • A quiet feed is a healthy feed. A 25-second subscription with real filters produced zero events — that’s expected, not broken. Real depegs and liquidity crunches aren’t hourly occurrences, so don’t design your UI around “an event should have arrived by now.”
  • It reconnects on its own. Treat onError as “reconnecting…”, not “the stream is dead.” Don’t tear your UI down on the first error.
  • Don’t rely on onError to validate your API key. Subscribing with a deliberately invalid key produced no error within 20 seconds in testing. If you need to confirm a key works, check it with a normal REST call (like simulateBestPositions) first — don’t wait on this socket to tell you.
  • All three filters fields are independent and optional — set only the ones you need.

Examples

Simplest subscription

Filtered to specific chains/protocols/pools

Cleaning up in React