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-blogs/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:
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:
@Override public void onDestroy() { mHubConnection.stop(); super.onDestroy(); }
Output Results :
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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Keshav Gupta
Keshav Gupta is Android Developer in Oodles, he always look forward for new tasks and new things to learn more.