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

3.2 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 09/06/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-0.1.0-preview2-35174 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.aspnet:signalr:0.1.0-preview2-35174'

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]

Known limitations

This is an early preview release of the Java client. There are many features that aren't supported yet. The following gaps are being worked on for future releases:

  • Only primitive types can be accepted as parameters and return types.
  • The APIs are synchronous.
  • Only the "Send" call type is supported at this time. "Invoke" and streaming of return values aren't supported.
  • Only the JSON protocol is supported.
  • Only the WebSockets transport is supported.

Additional resources