• Index:



    3.Steps Explanation

    4.Output Results





    Introduction :


    SignalR is a library for ASP.NET designers that rearranges the way toward adding continuous web usefulness to applications. Constant web usefulness is the capacity to have server code push substance to associated customers quickly as it ends up accessible, as opposed to having the server trust that a customer will ask for new information. So we are going to setup SignalR in our Android applications so an application can consistently get information without asking for server again for new information once it connected. For this, we will utilize SignalR libraries which you can discover visiting on a connection on last of the blog.


    Prerequisites :


    1.Android Studio 3.0+

    2.At least Java 7


    Steps Explanation :



    First copy these libraries found on this link in app/libs directory and add them as a module to your main app. By doing this, SignalR SDK will be installed in your application.

    Visit on this link to download libs




    Next, we will initialize the following classes in our activity class with global scope.HubConnection is an abstract class provided in SignalR that will make the connection to the defined Url.HubProxy will configure the connection to a proxy configured by method createHubProxy(String proxyName). Handler class will be used by the application to communicate to UI thread from another worker thread throughout the connection lifecycle.


        private HubConnection mHubConnection;
        private HubProxy mHubProxy;
        private Handler mHandler,



    Next, we will be initializing the AndroidplatformComponent  using abstract method for SignalR.Then we will initialize connection to a hosted URL.Then we will be creating a proxy with the hub registered.So will be passing hubname to createHubProxy(String hubname).Now hub connectio will be started.Code for this will look like:


    Platform.loadPlatformComponent(new AndroidPlatformComponent());
    String serverUrl = "YOUR URL NAME";
     mHubConnection = new HubConnection(serverUrl);
     mHubProxy = mHubConnection.createHubProxy(SERVER_HUB_CHAT_NAME);
     ClientTransport clientTransport = new ServerSentEventsTransport(mHubConnection.getLogger());
     SignalRFuture<Void> signalRFuture = mHubConnection.start(clientTransport);
     try {
     } catch (InterruptedException | ExecutionException e) {
         Log.e("SimpleSignalR", e.toString());



    Next, we will be invoking our subscription method names and pass objects required to them using invoke() method of HubConnection class. Then we will subscribe to events and setEventListener to them. Now final step will be overriding onMessageReceived() method which will be displaying data as received on the connection at the moment. If all things go well and all params provided by you is right, you will start receiving data in this method.Code for this part will be like:


      mHubProxy.invoke("your_method_name_1", new Object[]{"your object array”});
      mHubProxy.invoke("your_method_name_2", new Object[]{“your object array”});
      String CLIENT_METHOD_BROADAST_MESSAGE = "broadcastMessage";
              new SubscriptionHandler1<Object>() {
                  public void run(final Object msg) {
     Runnable() {
                          public void run() {
                              Toast.makeText(getApplicationContext(), msg.toString(), Toast.LENGTH_SHORT).show();
              , Object.class);
      mHubConnection.received(new MessageReceivedHandler() {
          public void onMessageReceived(final JsonElement json) {
              Log.e("onMessageReceived ", json.toString());
     Runnable() {
                  public void run() {
                      Toast.makeText(getApplicationContext(), json.toString(), Toast.LENGTH_SHORT).show();



    Finally, just stop the hub connection at onDestroy() of activity with respect to battery optimization factors otherwise connection will be still alive and thread will be working.


        public void onDestroy() {


    Output Results :

    You will be seeing logs messages in onMessageReceived() section for the data. If you are receiving data then you are properly connected with SignalR otherwise you need to check.



    However, you will be facing the problem while running your application in Android Oreo and above phones. As you will be given an error like "install failed matching no abis".Following error comes, when you are installing an application that has native libraries and it doesn't have a native library for your CPU architecture. To solve this just add these lines of code in app/build.gradle in android{} section.Now your application will work fine for Android Oreo as well.


     splits {
            abi {
                enable true
                include 'x86', 'armeabi-v7a'
                universalApk true
        packagingOptions {
            exclude 'lib/getLibs.ps1'
            exclude 'lib/'
            exclude 'lib/gson-2.2.2.jar'


    Conclusion :

    SignalR integration has allowed the developer to get continuous data stream from native technologies like Dotnet which will be helpful as a socket application in java client for native technologies.



    You can visit the link for further reference ""











Tags: android