Introduction¶
This guide will walk you through setting up a basic Best SignalR connection, invoking methods, and handling real-time communication.
Setting up the HubConnection¶
HubConnection
is your entry point to establish a connection with SignalR. Here's an example creating one:
Protocols¶
SignalR Core supports different protocols to encode its messages like Json and MessagePack. The safest is to use json, as that's the default encoder of the server. But if possible it's recommended to use MessagePack. More can be read about this under the Encoders topic. On this page the JsonProtocol
combined with the LitJsonEncoder
will be used, as these work out of the box.
A new HubConnection
object must be initialized with the uri of the server endpoint and with the protocol that the client want to communicate with:
HubConnection
's constructor can accept a HubOptions
instance too:
Connecting to the server¶
To start the protocol's connection process the StartConnect
and ConnectAsync
functions can be used.
Example
Invoking server methods¶
To invoke a method on a server that doesn't return a value, the Send
and SendAsync
methods can be used. Their first parameter is the name of the method on the server, than a parameter list can be passed that will be sent to the server.
Example
It can't cancel an already sent call, the server still going to process it, but the client going to give back controll sooner.
Invoking server functions¶
Invoking a server function can be done with the generic Invoke<TResult>
or InvokeAsync<TResult>
functions. TResult
is the expected type that the server function returns with.
Example
Invoke
returns with an IFuture<TResult>
that can be used to subscribe to various Invoke related events:
- OnSuccess: Callback passed for OnSuccess is called when the server side function is executed and the callback's parameter will be function's return value.
- OnError: Callback passed to this function will be called when there's an error executing the function. The error can be a client or server error. The callback's error parameter will contain information about the error.
- OnComplete: Callback passed to this function will be called after an OnSuccess or OnError callback.
InvokeAsync
returns with Task<TResult>
that can be awaited. As a second parameter a CancellationToken
can be added to cancel the call on client side.
Example
All Send
, Invoke
and theirs Async
counterparts are going to wait for a completion message from the server and their IFuture/Task completes when received it.
Server callable client methods¶
Clients can define server-callable methods using the generic and non-generic On
method. The non-generic On
can be used when the server-callable method has no parameter and the generic one for methods with at least one parameter.
Example
Server callable client functions¶
Server code that uses InvokeAsync
and expects an int
result:
Example
hub.Send("SomeMethod");
calls SomeMethod
on the server triggering the whole logic. The first callback for GetResult
receives the three arguments(description
, min
and max
) and must return with an int
result When the server receives result
it continues its execution and compares it its own randomValue
calling EndResult
on the client.
Disconnection and errors¶
To disconnect from the server and close the connection gracefully, the StartClose
/CloseAsync
functions can be used:
Example
Like StartConnect()
/ConnectAsync()
calles are paired with OnConnected
, StartClose()
/CloseAsync()
are paired with the OnClosed
event. These events are called even if their' Async peer is used.
OnClosed
is called only when the server or client initiates the closure of the Hub connection and could terminate gracefully. If there's a network error, only the OnError
event will be called as it always means that the client disconnected from the server!