Signal R in Android

Recently i developed a Android chatting application for the company called zupportdesk. I went through lot of difficulties since there is a very few online resources available for reference to implement signal R in Android, but somehow i figured it out by putting lot of time and effort on it. Now i thought of sharing what i have learned so that it will help some developers someday.

This tutorial will give you instruction on how to implement signal R in Android. Before getting started i expect you to have some knowledge on Android, Android Services and Signal R, therefore this tutorial is not for beginners. Ok lets get started

What is a signal R

SignalR is a new library used to add real-time web functionality to your applications. Signal R uses technology such as

  • web sockets
  • Event Source,
  • forever frame
  • long polling

Signal R is capable of selecting the best from those four technology depending on your internet connection and your application stability.

Signal R is used in application such as

  • Chatting application
  • Stock market application
  • Real-time gaming

It is also used in variety of sectors where ever the real-time communication is needed. to learn more about signal R visit signalr.net.

Required Libraries

  • gson-2.2.2 –  Download
  • signalr-client-sdk-android – Download
  • signalr-client-sdk – Download

Download these libraries and put it on your libs folder and then add it to your build.gradle as shown below.

Also we must ignore some files in the library

You app will work fine with out excluding these files, but when you upload it to the play store your application supported devices will be zero. Therefore its better to exclude the files now itself.

finally your build.gradle file will look some what similar to this

apply plugin: ‘com.android.application’

 

These type for application consumes huge amount of data and battery life of the device, therefore it is important to choose the best service that supports your application, I am going to use Android Bind service for this example. Because i want the application to run background task only when the application is open on the device.

To do the real time communication the services need to do three task.

  1. It should connect to Proxy hub and the keeps the connection continuously open.
  2. It should subscribe to the hub method , so that it can receive information from hub to the device, whenever the change or a new message is received to the hub.
  3. It should invoke the hub method when a device need to send messages to the hub

Example for hub method subscription are shown below

Example for invoke Hub methods are shown below

Finally You Service should look somewhat similar to this

In you Activity just call the Service

That’s it. if you do this properly you will have the connection between your signal R hub and your Android device. You can Check the connection information on ADT logs. From now on you just have to invoke and subscribe to hub method to send and receive messages.

 

Hope this helps. If you have any questions are suggestions, please feel free to ask. Happy Coding 🙂

I am a Software Engineer.

Leave a Reply

*

5 comments

  1. M karmakar

    ApplicationEnvironmentURL applicationEnvironment; what is that?

    • sathyabaman

      Its my Personal Utility class to get the URL of the Signalr Hub.

  2. ChrBicealo

    Viagra Y Salud Viagra Qualite Cialis Et Consommation D’Alcool Drugs viagra Cephalexin Strenght Using Cephalexin On Pregnant Dogs

Next ArticleCaught a pikachu - pokemon go