ASP.NET Core 2.2.0-preview1: SignalR Java Client

Sourabh

August 23rd, 2018

This post was authored by Mikael Mengistu.

In ASP.NET Core 2.2 we are introducing a Java Client for SignalR. The first preview of this new client is available now. This client supports connecting to an ASP.NET Core SignalR Server from Java code, including Android apps.

The API for the Java client is very similar to that of the already existing .NET and JavaScript clients but there are some important differences to note.

The HubConnection is initialized the same way, with the HubConnectionBuilder type.

HubConnection hubConnection = new HubConnectionBuilder () . withUrl ( "www.example.com/myHub" ) . configureLogging (LogLevel. Information ) . build ();

Just like in the .NET client, we can send an invocation using the send method.

hubConnection. send ( "Send" , input);

Handlers for server invocations can be registered using the on method. One major difference here is that the argument types must be specified as parameters, due to differences in how Java handles generics.

hubConnection. on ( "Send" , (message) -> { // Your logic here }, String . class );

Installing the Java Client

If you’re using Gradle you can add the following line to your build.gradle file:

implementation 'com.microsoft.aspnet:signalr:0.1.0-preview1-35029'

If you’re using Maven you can add the following lines to the dependencies section of your pom.xml file:

<dependency> <groupId> com.microsoft.aspnet </groupId> <artifactId> signalr </artifactId> <version> 0.1.0-preview1-35029 </version> </dependency>

For a complete sample, see https://github.com/aspnet/SignalR-samples/tree/master/AndroidJavaClient

This is an early preview release of the Java client so there are many features that are not yet supported. We plan to close all these gaps before the RTM release: