IOS implementing SignalR using Objective-C

For the past few weeks i been doing some research on SignalR in Objective C and i found very few resources available on the internet for implementing signlR in Objective-C. Therefore i thought of writing this tutorial, hoping it will help some developers one day.

In this tutorial i will explain you, how to  get connected to SingalR hub using IOS and Objective-C. This tutorial is not for beginners.

All Information about SignalR Objective-C library can be found in this Link.

Lets setup the environment first. flow the steps below

  1. First of all create a new IOS Objective C project.
  2. Close the project and navigate to the project directory Using Terminal.
  3. Create a new podfile and save the following code. (make sure to change your project name instead of signalRObjective2)
This will install required libraries such as AFNetworkingSocketRocket and as well as SignalR-ObjC

4. Now run the below command  in your terminal

pod install

. This will install all the required libraries and create a file with extension .xcworkspace in side your project folder.

5. Open the file yourproject.xcworkspace file

Now we got over environment setup , lets do the real coding to get connected to SignalR HUB.

In order to do a signal R communication, You need to understand three main factors in Signal R, They are explained below

  1. get connected – You must get connected to signalR Hub and keep the connection with out closing it.
  2. Subscribe to hub method – You must create a subscription to your hub method at the time of connection.
  3. Invoke hun method – You must invoke a hub method, when ever you want to send some message to the Hub and also receive the response through the subscribed method as explained before.

Now lets see how to implement it.

Connection

As you can see from the above code, you will have to tell the signalR hub URl and also the hub proxy name to the signalR-ObjectiveC library.

Then you need to subscribe to the Hub method as shown in the above code. serviceStatus is the hub method and it will return the response to testMethod which is in the client side.

Invoking a method in signalR hub is very simple as shown in the above code. And if you code dont have any parameters. Your invoke will be like this as shown below

That it. Over all Code will be as shown below

That is. If you have any problems are suggestions please be free to comment below, I will be happy to respond them.Thank you. Happy Coding 🙂

 

 

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 🙂