Blog

  • Index:

    1.Introduction

    2.Prerequisites.

    3.Steps Explanation

    4.Output Results

    5.Problems

    6.Conclusion

    7.Reference

     

    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 :

     

    Step1:

    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

    "https://s3.amazonaws.com/oodles-technologies1/blog-images/a026b311-8517-4357-986f-b59b0484c21c.zip"

     

    Step2:

    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,
    

     

    Step3:

    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);
     String SERVER_HUB_CHAT_NAME = "YOUR HUB NAME";
     mHubProxy = mHubConnection.createHubProxy(SERVER_HUB_CHAT_NAME);
     ClientTransport clientTransport = new ServerSentEventsTransport(mHubConnection.getLogger());
     SignalRFuture<Void> signalRFuture = mHubConnection.start(clientTransport);
     try {
         signalRFuture.get();
     } catch (InterruptedException | ExecutionException e) {
         Log.e("SimpleSignalR", e.toString());
         return;
     }
    

     

    Step4:

    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:

     

      Log.e("Connection",mHubConnection.getConnectionData());
      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";
    
      mHubProxy.on(CLIENT_METHOD_BROADAST_MESSAGE,
              new SubscriptionHandler1<Object>() {
                  @Override
                  public void run(final Object msg) {
                  
                     
              mHandler.post(new Runnable() {
                          @Override
                          public void run() {
                            
                              Toast.makeText(getApplicationContext(), msg.toString(), Toast.LENGTH_SHORT).show();
                          }
                      });
                  }
              }
              , Object.class);
      mHubConnection.received(new MessageReceivedHandler() {
          @Override
          public void onMessageReceived(final JsonElement json) {
              Log.e("onMessageReceived ", json.toString());
              mHandler.post(new Runnable() {
                  @Override
                  public void run() {
                      Toast.makeText(getApplicationContext(), json.toString(), Toast.LENGTH_SHORT).show();
                  }
              });
          }
      });
    

     

    Step5:

    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.

     

    @Override
        public void onDestroy() {
            mHubConnection.stop();
            super.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.

     

    Problems:

    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
                reset()
                include 'x86', 'armeabi-v7a'
                universalApk true
            }
        }
    
        packagingOptions {
            exclude 'lib/getLibs.ps1'
            exclude 'lib/getLibs.sh'
            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.

     

    Reference:

    You can visit the link for further reference "https://www.asp.net/signalr"

     

     

     

     

     

     

     

     

     

     

Tags: android