Integrating SignalR in Android Application

Posted By Keshav Gupta | 23-Dec-2018

SignalR is a library for ASP.NET designers. It rearranges continuous web usefulness to applications. Constant web usefulness is the capacity to have server code push substance. It helps in associating with customers so that they get more information content whenever needed.

 

Oodles Technologies is a leading android app development company in India. Our software development experts setup SignalR in Android applications so that application performs consistently, without having to ask servers for new information once connected. Our experts use SignalR libraries to evaluate visitor on a connection on a web page.

 

Prerequisites Tools we use:

 

1.Android Studio 3.0+

2.Java 7 or Java 8

 

Steps Explanation 

 

Step1:

Copying the libraries available on the link mentioned below

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

Push the add button to Add them as a module to your main app. By doing this, SignalR SDK and this is how it be installed to your app.

 

Step2:

The next step is to initialize classes in our activity class using global scope.HubConnection is an abstract class provided in SignalR for connecting to a defined Url.HubProxy. It configures a connection to the proxy using 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 initialize AndroidplatformComponent  using abstract method for SignalR.Then we will initialize connection to a hosted URL.After this we create a proxy with the hub registered. Android app developers pass 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:

Android software development services provided by us showcases 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:

In the end, we 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.
There are possibilities that designers will not be able to run their Android apps on Android Oreo versions. Errors may show up like: "installation failed matching no abis".Following error comes, when you are installing an application that has native libraries and its native libraries are not compatible with your CPU architecture. To solve this problem we add 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.

 

Are you planning to integrate your Android app with SignalR? Get in touch with the software development experts at Oodles Technologies for making your Android apps functional, robust and productive. Apart from creating feature-rich application our software developers provide assistance in blockchain solutions, IoT, video streaming and more.

 

 

 

 

 

Request for Proposal

Recaptcha is required.

Sending message..