For this tutorial, we’re going to use the SignalR client. SignalR is an ASP.NET library allowing real-time connections (such as chat/games). Due to the increasing popularity of C# and .NET on the backend, it’s common to work with it. If you need to connect to the backend using SignalR and want to learn the basics, this article is for you.

SignalR uses Hubs to connect the server with the client and vice versa. Because everything is real-time, the server can invoke client methods and vice versa.

First, let’s start by declaring the dependency. You can check the latest dependency here.

dependencies {
implementation "com.microsoft.signalr:signalr:$api_signalr_version"
}

First, you must create a connection object where you pass the URL to the required Hub.

val connection = HubConnectionBuilder
.create("signalr_hub_url")
.build()

Authentication Token

If the Hub requires authentication, you can pass the token using withAccessTokenProvider method.

val connection = HubConnectionBuilder
.create("signalr_hub_url")
.withAccessTokenProvider(
Single.defer { Single.just("token") }
)
.build()

Hubs are real-time, meaning they can send information once the connection is started. Be prepared to handle the returned data multiple times.

You’ll also need the Hub method name, which you should ask your backend for, or if you’ve access to the codebase, use the name specified in the Hub.

connection.on(
"your_hub_method_name",
{ data ->
// TODO handle data
},
String::class.java,
)
connection.start()

In the example, we receive data of type String from the hub method called your_hub_method_name. Remember to call connection.start() or the connection won’t start and you won’t receive any data.

If the Hub sends more than one object, modify your code by adding more types.

connection.on(
"your_hub_method_name",
{ id, name, surname ->
// TODO handle data
},
Int::class.java,
String::class.java,
String::class.java,
)
connection.start()

The order of parameter types is the same as the order of parameters, meaning:

  • id is of type Int
  • name is of type String
  • surname is of type String

Now, the Hub works in two ways: we receive data from a server, but we can also send something to it. Ensure you start the connection before invoking the method, or it won’t work.

Again, you’ll need the Hub method name, which you’ll have to ask the backend team for or find in the codebase.

// Make sure you're already connected
connection.invoke(
"your_hub_method_name",
data,
)

In the example above, we invoke the hub method called your_hub_method_name and provide data that it is of type ...args

When you’re finished, it’s time to stop the connection to preserve resources and avoid memory leaks.

connection.stop()

If you’re using Kotlin, you’ll probably want to receive data and emit it to a Flow you can do it with Callbackflow like this:

fun connectToChat() = callbackFlow {
connection.on(
"your_hub_method_name",
{ data ->
trySend(

)
},
String::class.java,
)
connection.start()
awaitClose {
connection.stop()
}
}

And that’s it! You finished the tutorial. I hope this helped you in some way.

Till next time! Happy learning!

Article written based on:

Source link