npm install react-use-signalr --save
If you haven't installed the signalR package yet, you have to add it manually:
npm install @microsoft/signalr --save
Use the HubConnectionBuilder descibed in the Microsoft documentation to build the connection.
const signalRConnection = new HubConnectionBuilder()
.withUrl("https://localhost:5001/chathub")
.withAutomaticReconnect()
.build();
To start the SignalR connection, pass the HubConnection instance to the useHub hook. The hook will provide the current hub state as well as an error value if the connection fail.
const { hubConnectionState, error } = useHub(signalRConnection);
useClientMethod(signalRConnection, "ReceiveMessage", (user, message) => {
[...]
});
const { invoke, loading, error } = useHubMethod(signalRConnection, "SendMessage");
[...]
invoke([...]);