locked
SignalR Java client - How to invoke a method immediately after hub.start()? RRS feed

  • Question

  • User573775647 posted
    chatHubConnection.start().;
    chatHubConnection.invoke(Void.class, "GetConnectionId");

    Simply put, this kind of code will throw NullPointerException at com.microsoft.signalr.HubConnection$ConnectionState.getNextInvocationId()
    The reason behind is rather easy to understand, it must be because the connection hasn't been established yet after the call hubConnection.start();
    But knowing the cause didn't help me to find out how to achieve my intention.

    I can see that the start() method returns a Completable object. That object has many rather suspicious methods like doOnComplete(), andThen(), etc...
    I tried to figure out how to use it but there was no hope.

    So, simply put, how to invoke a method immediately after HubConnection.start() in Java?

    Monday, May 6, 2019 3:16 AM

All replies

  • User573775647 posted

    After about an hour more kept trying in the hopeless, I finally can achieve my goal, so I will write it down here in case there is anyone might need it in the future.

    chatHubConnection.start().blockingAwait();
    chatHubConnection.invoke(Void.class, "GetConnectionId");

    This will make the chatHubConnection.invoke(Void.class, "GetConnectionId") run right after the connection establishes.

    Right now I can only think of this solution, but looks like its not a good approach as I make it blocked...

    EDIT:
    I am using ASP.NET Core SignalR. I use SignalR for my Android application. And I implemented the version 1.0.0 as showed in this tutorial ASP.NET Core SignalR Java client
    And from out of no where, today I encountered another exception, even when I just only write "chatHubConnection.start()". The issue is described in this link [SignalR] Java Client NullPointerException
    As the hint solution in the above issue, I have implemented the latest version of SignalR, that is 3.0.0-preview-19075-0444. And then, the problem's gone.
    After implementing the lastest version, I tried writing the code that threw exception yesterday:

    chatHubConnection.start();
    chatHubConnection.invoke(Void.class, "GetConnectionId");

    This time, it still threw exception, but it described the error in a more detail, it said: "The 'invoke' method cannot be called if the connection is not active.". Very good :)
    Then, I tried writing the line of code that I thought it should have worked yesterday:

    hubConnection.start().doOnComplete(() -> hubConnection.invoke(Void.class, "GetConnectionId"));

    And guess what? It actually works. That makes a whole sense and beautiful too.

    Monday, May 6, 2019 3:56 AM
  • User61956409 posted

    Hi daophilac,

    Welcome to ASP.NET forums.

    Are you using ASP.NET SignalR? or ASP.NET Core SignalR? If possible, please tell us which SignalR Java client package you are using to setup client application.

    With Regards,

    Fei Han

    Monday, May 6, 2019 6:58 AM
  • User573775647 posted

    Hi daophilac,

    Welcome to ASP.NET forums.

    Are you using ASP.NET SignalR? or ASP.NET Core SignalR? If possible, please tell us which SignalR Java client package you are using to setup client application.

    With Regards,

    Fei Han

    Hi, please see my edited answer

    Wednesday, May 8, 2019 6:12 PM