title: How to use signalr in Android
body: <p>I am trying to integrate <code>signalR</code> in <code>android</code> app but no luck. I've been looking at various links but none of them provide proper information about implementation.</p>
  
  <p>I've the following questions.</p>
  
  <ul>
  <li><code>SignalR</code> integration has to be done inside Service/Intent Service?</li>
  <li>If we want to receive response via same calling method then how to get?</li>
  </ul>
  
  <p>I've added three libraries i.e <code>signalr android</code>,<code>signalr client</code> and <code>gson</code> but unable to understand how code works, no proper documentation is available to understand the code.</p>
  
  <p>Some of the questions asked but not much information</p>
  
  <p><a href="https://stackoverflow.com/questions/25753423/signalr-in-android-studio">SignalR in Android Studio</a>
  <a href="https://stackoverflow.com/questions/24012573/unable-to-implement-p2p-chat-using-signalr-in-android">Unable to implement p2p chat using SignalR in Android</a></p>
  
  <p>If anyone experienced in signal for native apps, it would be very helpful for me.</p>
  
  <p><strong>Update</strong></p>
  
  <pre><code>    public class SignalRService extends Service {
  
  
      private static final String TAG = "Service";
      private HubConnection mHubConnection;
      private HubProxy mHubProxy;
      private Handler mHandler; // to display Toast message
      private final IBinder mBinder = new LocalBinder(); 
  
      private SharedPreferences sp;
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          Utility.showLog(TAG, "Service Created");
  
          sp = getSharedPreferences(Utility.SHARED_PREFS, MODE_PRIVATE);
          mHandler = new Handler(Looper.myLooper());
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          int result = super.onStartCommand(intent, flags, startId);
          startSignalR();
          return result;
      }
  
      @Override
      public IBinder onBind(Intent intent) {
  
          startSignalR();
          return mBinder;
      }
  
      /**
       * Class used for the client Binder.  Because we know this service always
       * runs in the same process as its clients, we don't need to deal with IPC.
       */
      public class LocalBinder extends Binder {
          public SignalRService getService() {
              // Return this instance of SignalRService so clients can call public methods
              return SignalRService.this;
          }
      }
  
      /**
       * method for clients (activities)
       */
      public void sendMessage() {
  
          String SERVER_METHOD_SEND = "iAmAvailable";
          final String string = new String();
  
          mHubProxy.invoke(new String(), SERVER_METHOD_SEND, sp.getString("user_id", null), sp.getString("pass", null), "TransMedic").done(new Action() {
              @Override
              public void run(Object o) throws Exception {
  
                  Utility.showLog(TAG, o.toString());
  
              }
  
  
          }).onError(new ErrorCallback() {
              @Override
              public void onError(Throwable throwable) {
  
              }
          });
      }
  
      private void startSignalR() {
  
          Platform.loadPlatformComponent(new AndroidPlatformComponent());
  
          String serverUrl = "http://transit.alwaysaware.org/signalr";
  
          mHubConnection = new HubConnection(serverUrl);
  
          String SERVER_HUB_CHAT = "ChatHub";
  
          mHubProxy = mHubConnection.createHubProxy(SERVER_HUB_CHAT);
  
          ClientTransport clientTransport = new ServerSentEventsTransport(mHubConnection.getLogger());
  
          SignalRFuture&lt;Void&gt; signalRFuture = mHubConnection.start(clientTransport);
  
  
          try {
  
              signalRFuture.get();
  
          } catch (InterruptedException | ExecutionException e) {
  
              e.printStackTrace();
              return;
  
          }
  
          sendMessage();
      }
  
      @Override
      public void onDestroy() {
  
          mHubConnection.stop();
          super.onDestroy();
  
      }
  }
  </code></pre>