use-signalr
Reference

API reference

Every export of @dammers/use-signalr-solid with its signature, return value, and lifetime.

Everything on this page comes from @dammers/use-signalr-solid. The package has no default export.

createSignalRClient takes the contract and returns the provider and the members below. Its signature is:

function createSignalRClient<const H extends Record<HubString, HubDef>>(
  config: SignalRClientConfig<H>,
): {
  SignalRProvider: (props: SignalRProviderProps<Hub>) => JSX.Element;
  useSignalR: () => SignalRPublicContext<T>;
  useHubConsumer: (hub: Hub) => void;
  useSignalREffect: (hub, event, handler) => void;
  useSignalRInvoke: (hub, method, options?) => (...args) => Promise<Return>;
  useSignalRSend: (hub, method) => (...args) => Promise<boolean>;
  useSignalRTeardown: (hub, method, options?) => (...args) => Promise<boolean>;
  useHubStatus: (hub) => Accessor<HubConnectionStatus>;
  useOnReconnected: (hub, callback) => void;
};

The call reads the keys of config.hubs as the hubs. It reads the event() and method() declarations of each hub as the contract. Each member of the result is typed against that contract. T is the contract and Hub is keyof T & HubString.

Call it once per contract, at module level. The call does no I/O. The members of one call work together: use the hooks below the provider from the same call.

Members

The members of the result and the two contract helpers are in this table:

NameSignatureReturnsLifetimeKeeps a lazy hub alive
SignalRProvider(props: SignalRProviderProps)The children, inside the context.The owner of the provider. It stops the session when Solid disposes the owner.No. It starts eager hubs only.
useSignalR()SignalRPublicContext with getConnection, getStatus, and waitForConnection.The setup of the component. It holds no resource.No
useHubConsumer(hub)voidThe owner of the component. It acquires the hub at setup and releases it on cleanup.Yes
useSignalREffect(hub, event, handler)voidThe owner of the component. It removes the handler on cleanup.Yes
useSignalRInvoke(hub, method, options?: InvokeOptions)A stable function. It resolves with the return value of the method.The owner of the component. On cleanup it aborts calls that wait, unless keepAliveOnUnmount is true.Yes
useSignalRSend(hub, method)A stable function. It resolves true when it sent the call, false when the hub was not connected or the send failed. It never throws.The owner of the component. The function stays safe to call after cleanup.Yes
useSignalRTeardown(hub, method, options?: TeardownOptions)A stable function. It waits for the hub, then sends. It resolves true when it sent the call, false when the hub did not connect in time, the session was disabled, or the send failed. It never throws.The function outlives the component. It holds a lazy hub open until the send ends.Yes, while the owner lives
useHubStatus(hub)An Accessor<HubConnectionStatus>. A tracking scope that reads it runs again only when the status of this hub changes.The owner of the component.Yes
useOnReconnected(hub, callback)void. The callback runs after each reconnect, not after the first connect.The owner of the component. It removes the callback on cleanup.Yes
event<A extends unknown[] = []>()EventDef<A>. A is the tuple of handler arguments.None. It carries types only and returns an empty object at run time.No
method<A extends unknown[] = [], R = void>()MethodDef<A, R>. A is the tuple of arguments and R is the resolved return type.None. It carries types only and returns an empty object at run time.No

Each hook reads its arguments once, at setup. The hub, the method, and options are not reactive. The functions that useSignalRInvoke, useSignalRSend, and useSignalRTeardown return keep their identity for the life of the owner.

A handler that you pass to useSignalREffect or useOnReconnected is a closure. It reads signals when it runs, so it sees current values.

Config lists InvokeOptions and TeardownOptions. The call rules are in Calls. The status values are in Connection status.

Types

All types are exports of the package. Most come from @dammers/use-signalr-core.

TypeMeaning
SignalRProviderPropsThe props of SignalRProvider. It adds children: JSX.Element to the shared provider props.
SignalRClientConfigThe argument of createSignalRClient.
PerHubConfigThe options that one hub can set.
HubDefOne entry of hubs: PerHubConfig plus events and methods.
EventDefThe result of event().
MethodDefThe result of method().
HubStringA hub path. It is a string that starts with /.
HubContractThe events and methods of one hub, as function types.
SignalRContractA map from hub path to HubContract.
InferContractDerives the SignalRContract from the hubs object.
EventNameThe event names of one hub.
MethodNameThe method names of one hub.
EventArgsThe argument tuple of one event.
MethodArgsThe argument tuple of one method.
MethodReturnThe resolved return type of one method.
HubConnectionStatus"idle" | "connecting" | "connected" | "reconnecting" | "disconnected".
SignalRErrorInfoThe info argument of onError. source is "connection" or "callback".
SignalRPublicContextThe result of useSignalR.
InvokeOptionsThe options of useSignalRInvoke.
TeardownOptionsThe options of useSignalRTeardown.
ReconnectConfigboolean, an array of delays in ms, or a custom retry policy.
HttpOptionsThe SignalR HTTP options, without accessTokenFactory.
HubProtocolConfigA hub protocol, or a function that returns one.
ConfigureBuilderA function that changes the connection builder.
BuilderContextThe second argument of ConfigureBuilder: hub and baseUrl.

Errors

The errors that a hook or a call can throw are in Troubleshooting.

On this page