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:
| Name | Signature | Returns | Lifetime | Keeps 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) | void | The owner of the component. It acquires the hub at setup and releases it on cleanup. | Yes |
useSignalREffect | (hub, event, handler) | void | The 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.
| Type | Meaning |
|---|---|
SignalRProviderProps | The props of SignalRProvider. It adds children: JSX.Element to the shared provider props. |
SignalRClientConfig | The argument of createSignalRClient. |
PerHubConfig | The options that one hub can set. |
HubDef | One entry of hubs: PerHubConfig plus events and methods. |
EventDef | The result of event(). |
MethodDef | The result of method(). |
HubString | A hub path. It is a string that starts with /. |
HubContract | The events and methods of one hub, as function types. |
SignalRContract | A map from hub path to HubContract. |
InferContract | Derives the SignalRContract from the hubs object. |
EventName | The event names of one hub. |
MethodName | The method names of one hub. |
EventArgs | The argument tuple of one event. |
MethodArgs | The argument tuple of one method. |
MethodReturn | The resolved return type of one method. |
HubConnectionStatus | "idle" | "connecting" | "connected" | "reconnecting" | "disconnected". |
SignalRErrorInfo | The info argument of onError. source is "connection" or "callback". |
SignalRPublicContext | The result of useSignalR. |
InvokeOptions | The options of useSignalRInvoke. |
TeardownOptions | The options of useSignalRTeardown. |
ReconnectConfig | boolean, an array of delays in ms, or a custom retry policy. |
HttpOptions | The SignalR HTTP options, without accessTokenFactory. |
HubProtocolConfig | A hub protocol, or a function that returns one. |
ConfigureBuilder | A function that changes the connection builder. |
BuilderContext | The second argument of ConfigureBuilder: hub and baseUrl. |
Errors
The errors that a hook or a call can throw are in Troubleshooting.