/
v3.0.11Get access

Subscribe

Opens a live subscription for the given Swarm method. Data updates in place as the platform pushes changes, and the subscription is torn down automatically when the component unmounts or the dependencies change.

Show as
Import from@softconstruct/betchain-core-client/hooks
Use it whenAnything that moves: live odds, scores, match state, jackpot values.

Signature

.ts
useSwarmSubscription(functionName: TFn, options: {
  params?: SwarmSubscriptionParamsOfFn<TFn>;
  dependencies?: DependencyList;
  shouldWait?: boolean;
}): {
  data; hasError; message; isLoading;
}

Options

Passed as the second argument, except for useInitSDK which takes a single config object.

NameTypeRequiredDescription
paramsParamsOfFunction<TFn>NoArguments forwarded to the method. Defaults to an empty object. Typed from the method you pass in.
dependenciesDependencyListNoValues that re-trigger the call when they change, like a useEffect dependency array. Defaults to an empty array, so the call runs once.
shouldWaitbooleanNoWhen true the hook does not fire on mount. Use it to defer a call until a prerequisite (an id, a logged-in session) is ready. Defaults to false.

Returns

NameTypeRequiredDescription
dataSwarmSubscriptionResultOfFn<TFn>NoLatest pushed payload. Undefined until the first message arrives.
isLoadingbooleanNoTrue until the first message is received.
hasErrorbooleanNoTrue when the stream reported an error.
messagestringNoError text from the stream, empty otherwise.

Example

.ts
1import {2  subscribeToUpcomingEventsWithSport3} from '@softconstruct/betchain-core-client/swarm';4import {5  useSwarmSubscription6} from '@softconstruct/betchain-core-client/hooks';7 8const {9  data,10  isLoading,11  hasError12} = useSwarmSubscription(subscribeToUpcomingEventsWithSport, {13  params: {14    sportAlias,15    limit: 20,16    isLive: false17  },18  // resubscribes whenever the selected sport changes19  dependencies: [sportAlias]20});

Notes

Unsubscribes for youThe effect cleanup calls unsubscribe, so you never leak a stream when the component unmounts or the dependencies change.
Do not pass a listenerThe hook injects its own listener and writes each payload into data. Pass only the method parameters.
Was this page helpful?