AspNetCore.Docs/aspnetcore/signalr/java-client.md

3.7 KiB

title author description monikerRange ms.author ms.custom ms.date uid
ASP.NET Core SignalR Java client mikaelm12 Learn how to use the ASP.NET Core SignalR Java client. >= aspnetcore-2.2 mimengis mvc 10/18/2018 signalr/java-client

ASP.NET Core SignalR Java client

By Mikael Mengistu

The Java client enables connecting to an ASP.NET Core SignalR server from Java code, including Android apps. Like the JavaScript client and the .NET client, the Java client enables you to receive and send messages to a hub in real time. The Java client is available in ASP.NET Core 2.2 and later.

The sample Java console app referenced in this article uses the SignalR Java client.

View or download sample code (how to download)

Install the SignalR Java client package

The signalr-1.0.0-preview3-35501 JAR file allows clients to connect to SignalR hubs. To find the latest JAR file version number, see the Maven search results.

If using Gradle, add the following line to the dependencies section of your build.gradle file:

implementation 'com.microsoft.signalr:signalr:1.0.0-preview3-35501'
implementation 'io.reactivex.rxjava2:rxjava:2.2.2'

If using Maven, add the following lines inside the <dependencies> element of your pom.xml file:

[!code-xmlpom.xml dependency element]

Connect to a hub

To establish a HubConnection, the HubConnectionBuilder should be used. The hub URL and log level can be configured while building a connection. Configure any required options by calling any of the HubConnectionBuilder methods before build. Start the connection with start.

[!code-javaBuild hub connection]

Call hub methods from client

A call to send invokes a hub method. Pass the hub method name and any arguments defined in the hub method to send.

[!code-javasend method]

Call client methods from hub

Use hubConnection.on to define methods on the client that the hub can call. Define the methods after building but before starting the connection.

[!code-javaDefine client methods]

Add logging

The SignalR Java client uses the SLF4J library for logging. It's a high-level logging API that allows users of the library to chose their own specific logging implementation by bringing in a specific logging dependency. The following code snippet shows how to use java.util.logging with the SignalR Java client.

implementation 'org.slf4j:slf4j-jdk14:1.7.25'

If you don't configure logging in your dependencies, SLF4J loads a default no-operation logger with the following warning message:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

This can safely be ignored.

Known limitations

This is a preview release of the Java client. Some features aren't supported:

  • Only the JSON protocol is supported.
  • Only the WebSockets transport is supported.
  • Streaming isn't supported yet.

Additional resources