Getting started with the Java SignalR SDK

Microsoft has just released a Java SignalR SDK which allows Java and Android clients to access ASP.NET SignalR back ends. The library is open source, and currently it is up to you to build the .jar packages yourself. For your convenience I’ve uploaded my built .jar files. Given that this library as of yet is still fairly undocumented, and I spent quite some time on getting it up and running, I figured I’d provide a short introduction tutorial here. I found the C# SignalR client documentation to be the most useful while figuring out the API as the classes and methods overall seem to correspond. The tests in the GitHub repository are another useful resource. I’ll mainly be focusing on the specific differences in the Java library, and refer you to the original documentation for more elaborate information.

We will create a simple Hub supporting bidirectional communication. A quick and easy way to get a C# back end up and running for the purpose of this tutorial is to self-host a SignalR server in a simple console or WPF application. The following C# code shows a simple hub with one method which can be called by the Java client, SendMessage.

public class MessageHub : Hub
	public static event Action<string, string> MessageReceived = delegate { };

	public void SendMessage( string name, string message )
		MessageReceived( name, message );

I added a static event handler which can be consumed by the console or WPF front end to display the received message. SendMessage is called from a different thread, so in case you want to update the UI, don’t forget to use a dispatcher!

MessageHub.MessageReceived  += ( name, message ) => _dispatcher.Invoke(
    () => { MessageBox.Content = String.Format( "{0} said {1}", name, message ); } );

Onwards to the client side code! To set up a Java project which can use the SignalR client API, add the ‘signalr-client-sdk.jar’ and ‘gson-2.2.2.jar’ to the project build path. For Android projects the references need to be added differently. Drag the two jars, and the additional ‘signalr-client-sdk-android.jar’ to the libs folder in Eclipse. By doing so they will automatically be added as Android libraries.

As mentioned before, the SignalR Java client follows the same structure as the C# client API, thus setting up a connection is quite similar. However, for Android applications an additional platform component needs to be loaded as shown below; also don’t forget to add internet permission to your manifest file, or you will receive a SocketException when trying to connect.

Platform.loadPlatformComponent( new AndroidPlatformComponent() );
// Change to the IP address and matching port of your SignalR server.
String host = "";
HubConnection connection = new HubConnection( host );
HubProxy hub = connection.createHubProxy( "MessageHub" );

This simply configures the connection. Establishing the actual connection is somewhat different than the C# documentation due to limitations of Java. To mimic language support for async in C#, the SignalRFuture class is introduced. Asynchronous operations return an instance of this class, without actually performing any real work yet. What follows is example code of how to start the connection synchronously by calling get() on the SignalRFuture. Don’t forget to cleanly stop() the connection when shutting down the application.

SignalRFuture<Void> awaitConnection = connection.start();
try {
} catch (InterruptedException e) {
	// Handle ...
} catch (ExecutionException e) {
	// Handle ...

In case your server is up and running, you should now be ready to start listening to and submitting messages. Again, listening to events in C# is more straightforward since it supports lambdas as the C# client API documentation demonstrates.

stockTickerHubProxy.On<Stock>( "UpdateStockPrice", stock => Console.WriteLine(
    "Stock update for {0} new price {1}", stock.Symbol, stock.Price) );

Doing something similar in Java requires elaborate inline anonymous classes, or the creation of a handler class per event you want to listen to. Suppose the server would send a message “context.Clients.All.UpdateStatus( “Online” );”, handling this the ‘C# way’ would like as follows:

hub.on( "UpdateStatus",
    new SubscriptionHandler<String>() {
        public void run( String status ) {
           // Since we are updating the UI,
           // we need to use a handler of the UI thread.
           final String fStatus = status;
  new Runnable() {
               public void run() {
                   statusField.setText( fStatus );
           } );
, String.class );

Therefore, Microsoft has seemingly added a method not available in the C# API to the Java client. Calling hub.subscribe( listener ) where listener is an object implementing corresponding methods for every available incoming event is a much more straightforward way to listen to messages. Under the covers this uses reflection to hook everything up correctly. These methods need to be public!

hub.subscribe( this );
public void UpdateStatus( String status )
	final String fStatus = status; Runnable(){
		public void run() {
			statusField.setText( fStatus );

More complex types work as well, as long as it is supported by JSON. I don’t know the specifics, but this will be dependent on the SignalR server and the gson library used by the SignalR Java client. The following code prepares the client to call a new method on the server which you can add to the MessageHub: “public void SendCustomType( CustomType object ) { … }“.

// A simple C# class which can be sent over SignalR.
public class CustomType
	public string Name;
	public int Id;
// The same type as defined in Java.
public class CustomType
	public String Name;
	public int Id;

Finally, calling the earlier SendMessage() and the newly added SendCustomType() can be done as follows. As you can see, remote method invocation again returns a SignalRFuture, and a subsequent get() is needed.

try {
    hub.invoke( "SendMessage", "Client", "Hello world!" ).get();
    hub.invoke( "SendCustomType",
        new CustomType() {{ Name = "Universe"; Id = 42; }} ).get();
} catch (InterruptedException e) {
	// Handle ...
} catch (ExecutionException e) {
	// Handle ...

Similarly, you can simply pass custom objects from the server to the client by adding the custom type as a method parameter: “public void SomeMethod( CustomType bleh ) { … }

  1. #1 by Anders Bech Mellson on April 1, 2014 - 8:53 am

    Nice write-up, thank you for the tutorial.

  2. #2 by Gadael Sedubun on May 9, 2014 - 12:33 pm

    can you provide the source code for this tutorial?
    kinda confuse how to use this signalR library..

  3. #4 by lal on August 12, 2014 - 7:27 pm

    Thanks for the great tutorial. I m getting following Console message, but event is not firing

    Verbose – serverSentEvents – Found new data:data: {}
    Verbose – serverSentEvents – Trigger onData: {}
    Verbose – HubConnection – Received data:

    I had tried two ways
    hub.on( “testMessage”, new SubscriptionHandler1() {
    public void run(CustomMessage msg) {
    Log.d(“result := “, msg.text);
    Toast.makeText(MainActivity.this, msg.text, Toast.LENGTH_LONG).show();
    }, CustomMessage.class);


    Handler handler = new Handler();
    public void addMessage( CustomMessage msg ) {

    final String fStatus = msg.text; Runnable(){
    public void run() {

    Toast.makeText(MainActivity.this, fStatus, Toast.LENGTH_LONG).show();


  4. #7 by windarco on August 26, 2014 - 5:32 pm

    How do I use Java reflection with hub.subscribe(“MessageFromServer”), cause I am struggling to let my android client get triggered by a message send from the server to client.

    • #8 by Steven Jeuris on August 26, 2014 - 5:50 pm

      What do you mean “how do I use Java reflection”? As I stated, reflection is used under the covers by the Java SignalR SDK. You do not need to implement this yourself when using `hub.subscribe`. However you seem to be confusing `hub.on` with `hub.subscribe`. `hub.on` accepts a method name as you seemingly suggest to be “MessageFromServer”. `hub.subscribe` as I described in this post accepts an object which will listen to incoming messages. Messages are automatically coupled to public methods of the object.

  5. #9 by Pairkshit singh shekhawat on August 29, 2014 - 1:19 pm

    Hi I am trying to create chat in android. using your blog. can you tell me what are parameters in hub.invoke method and how to get message in details

  6. #11 by Anil Kumar on October 6, 2014 - 5:29 pm

    i am unable to do connect Signal R with Samsung device can u please tell me Solution while with other device its working fine

  7. #12 by jhansi on October 30, 2014 - 9:15 am

    i am getting negotiation exception

  8. #13 by Bangar Reddy on October 30, 2014 - 9:18 am

    @steven we are some little bit confused by seeing all .net & Android code here. If you have any working android sample can you please share for me.

  9. #14 by Naimul Mefraz Khan on December 9, 2014 - 1:02 am

    hi Steven,

    I have been trying to get the java client working for the past week, can’t get past the NegotiatonException. I have posted a question over at stackoverflow, can you help me with this?

    P.S. the question is for the client-tests that ship with the git repo itself, I have also tried the method described here (start from scratch), same issue. Below is the pastebin from your code (the connection bit):

    D/javaclient(27057): java.util.concurrent.ExecutionException: microsoft.aspnet.signalr.client.transport.NegotiationException: There was a problem in the negotiation with the server

  10. #15 by Naimul Mefraz Khan on December 9, 2014 - 6:49 pm

    Adding to my answer in case someone else runs into it. It was a crappy mistake on my part, jumped into java client without understanding signalr properly! The Hub class on the server and the Hub proxy name has to be the same. So on the server side the class:

    public class MessageHub : Hub

    And on the client side

    HubProxy hub = connection.createHubProxy( “MessageHub” );

    And voila! Thanks for the blog.

  11. #16 by Hunter on December 11, 2014 - 4:29 am

    Could you help me ~
    How to Get a Value by public SignalRFuture invoke(final Class resultClass, final String method, Object… args)
    my code as below:

    Class resultClass = null;
    SignalRFuture str = hub.invoke(resultClass,
    “HelloWorld2”, “123”);
    str.done(new Action() {
    public void run(String arg0) throws Exception {
    Log.i(“0.03”, “arg0=” + arg0);// i can’t receive the result
    }).onError(new ErrorCallback() {
    public void onError(Throwable error) {
    Log.i(“0.03”, “error=” + error.toString());

    by other way, as below:

    new SubscriptionHandler1() {
    public void run(String msg) {
    Log.i(“0.03”, “Result” + msg); // i can receive the result
    }, String.class);

    please help me, how to receive the return value by
    How to Get a Value by public SignalRFuture invoke(final Class resultClass, final String method, Object… args)

  12. #17 by ralessi on January 25, 2015 - 5:15 pm

    How you create the jars ? i have tried to recompile with Android Studio the latest sources , using GSON 2.3, but with these jars and GSON 2.3 i get the error “could not find class microsoft.aspnet.signalr.client.transport.websocket transport” in logcat while creating the HubProxy

    • #18 by Steven Jeuris on January 25, 2015 - 6:39 pm

      I also experienced some troubles building the jars, but unfortunately do not remember the specifics. If you manage to build them it might be worthwhile sharing them.

  13. #19 by agrumiro on February 18, 2015 - 2:42 pm

    Hi, how you generate signalr-client-sdk-android.jar ? i’ve downloaded source code from github, import the project in AndroidStudio and build the project. The result is only aar file for signalr-client-sdk-android module.
    Thank you

    • #20 by Steven Jeuris on February 18, 2015 - 3:23 pm

      I also experienced some troubles building the jars, but unfortunately do not remember the specifics. If you manage to build them it might be worthwhile sharing them.

    • #21 by Chantell Osejo on May 8, 2015 - 8:14 pm

      This is late, but you have to use the aar (import it as a new module, File -> New -> New Module, select at the bottom the option for “Import .JAR or .AAR Package”) and the jar. Add the .jar (client-sdk, NOT the one for Android, that will be the .AAR) to your /libs folder, and then be sure to right-click the jar in Android Studio and select the “Add as library…” option. Finally, add in your build.gradle the following lines:

      compile project(‘:signalr-client-sdk-android-release’)
      compile files(‘libs/signalr-client-sdk.jar’) [this may be added automatically with the above “Add as library…” step]

      This will get you to building, at least.

  14. #22 by saint on August 29, 2015 - 4:07 pm

    great tuttorial thenks however i have an issue with hub.invoke( “Send”, “Android”,”hie”).get(); im getting a java.lang.null pointer exeption even though my andorid client has succesfully connected to the signalr Server.. i know this because onconnected server method fires

  15. #24 by Nuri YILMAZ on September 7, 2015 - 2:57 pm

    tutorial based on another failed microsoft open source tale! who care this project? last 7 months there is no contributor, 32 issues collected but no one closed them.

  16. #25 by Jaygarcia on October 16, 2015 - 11:25 am

    Very nice I followed this tutorial..,works great .,but only on emulator..
    But in device error is install failed CPU abi incompatible.,anye can help me out,.

    My device is 4.4.2 KitKat.., cheers thanks

  17. #26 by Anup on November 22, 2015 - 3:43 am

    Hi, I got the java client working, received message is working but I cannot send the message. My code looks like

    globalVariable.getmProxy().invoke(“SendToOperators”, “21”, “test message”).done(new Action() {

    public void run(Void obj) throws Exception {
    }).onError(new ErrorCallback() {
    public void onError(Throwable throwable) {

    I tried with get() too. Like..

    globalVariable.getmProxy().invoke(“SendToOperators”, “21”, “test message”).get();

    It throws exception…

    11-22 01:30:16.202 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ Expected EOF at line 1 column 10
    11-22 01:30:16.202 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at
    11-22 01:30:16.202 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at microsoft.aspnet.signalr.client.transport.TransportHelper.processReceivedData(
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at microsoft.aspnet.signalr.client.Connection.processReceivedData(
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at microsoft.aspnet.signalr.client.Connection.access$0(
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at microsoft.aspnet.signalr.client.Connection$1.onData(
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at microsoft.aspnet.signalr.client.transport.HttpClientTransport$2.onResponse(
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ Caused by: Expected EOF at line 1 column 10
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ at
    11-22 01:30:16.212 26464-27292/com.eb5.uvexa.uvexadriver W/System.err﹕ … 8 more

    I have a working dot net client so I can say that the code is fine.

    I will appreciate your help.


  18. #27 by Dinuka PJ on February 8, 2016 - 10:37 am

    Facing a huge issue here. I can’t seem to set this up in my project. I followed every step. Here’s my Stack overflow question:

  19. #28 by jalalbarzi on October 23, 2017 - 7:32 pm

    Hello everyone, I just need a quick response plus help on how to send a message to specific client as a client from java, for instance I am connected c# back end and I have connected to the hub now I want to instruct the hub in java that, the message I am sending you is to send to a specific client.
    How to do it ???

  1. Dictionary as query string parameter in SignalR Android Client
  2. How to use: Azure Service Bus – Notification hub, Azure hosted SignalR Hub with .NET clients, Android clients and web JavaScript clients | For the love of problems :)
  3. Android Studio: Execution fails when I add SignalR client library « news-Knowlage FeeD

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: