WebSockets Implementation With SignalR And Xamarin.Forms
What is SignalR?
ASP.NET SignalR is a .NET framework focus on real-time communications between clients and server, the server can push content instantly to the connected clients when is needed rather than having a server waiting till new client request. so , using SignalR client and server could call method on each other at any time, like image below:
(Image Source: https://docs.microsoft.com/en-us/aspnet/signalr/overview/getting-started/introduction-to-signalr)
SignalR can be used to add any real-time functionality you want since a chat to any other real-time communication you web or mobile app need and is really easy to use.
SignalR, WebSocket And Transport Falls Back
One of the most interesting features of the SignalR Framework is the possibility to support all real time communication supported for the different clients, for example, if one of your client doesn’t support WebSocket then the connection could be made using Long Polling or Server Sent-Event without any other adjustment on the server.
SignalR is an abstraction over some of the transports that are required to do real-time work between client and server. A SignalR starts as HTTP, and try to make a WebSocket connection if it is available. WebSocket is the ideal transport for SignalR, since it makes the most efficient use of server memory, has the lowest latency, and has the most underlying features (such as full duplex communication between client and server), but it also has the most stringent requirements: WebSocket requires the server to be using Windows Server 2012 or Windows 8, and .NET Framework 4.5 and not all clients support it therefore If these requirements are not met, SignalR will attempt to use other transports to make its connections.
SignalR uses the WebSocket transport and although you could certainly write your application using WebSocket directly, using SignalR means that a lot of the extra functionality you would need to implement will already have been done for you. Most importantly, this means that you can code your application to take advantage of WebSocket without having to worry about creating a separate code path for older clients. SignalR also shields you from having to worry about updates to WebSocket, since SignalR will continue to be updated to support changes in the underlying transport, providing your application a consistent interface across versions of WebSocket.
The Example used for this is in the next url: https://github.com/edgarleonardo/ChatServerWithSignalR
What is Xamarin?
Xamarin is a mobile framework based on mono that help you develop applications for the major three platforms using the .NET stack and C# as programming language. Among its advantages are the possibility to share almost the 90% of the code but this depends on the type of application you want to develop and how it needs to interact with the different component of each mobile platform.
Xamarin.Forms is cross platform UI toolkit to share UI between the three majors mobile platforms (Android, IOS and Windows Phone).
Creating the Xamarin.Form App For Use WebSockets With SignalR
Recently I was developing an application and born the need to use Websocket as transport layer on my app. The BackEnd of the app was developed using ASP.NET and SignalR as framework to handle the real-time communications.
When I use the nuggets for make possible the connection to my SignalR server using Websocket I realize that the current signalR client library was not able to use Websocket as transport layer because the “System.Net.WebSockets” was not able for PCL and that the reason you can’t do the connection unless developing the websocket implementation on each platform to be able to use it. I used the interface IClientTransport offered by signalR.Client nugget to integrate my implementation with the current signalR library and make possible the WebSocket communication.
I used Dependency Inversion Principle and Dependency class from Xamarin.Form to get the communication between each platform and my main PCL in order to make possible the implementation.
Here is exactly all step I did:
First Create the implementation on my PCL with these Classes and Interfaces:
IWebSocket: This is the interface that abstract the websocket behaviors from my PCL and the element used for the dependency inversion between PCL and each platform.
IConnectionHolder: this interface abstract the platform of several dependency needed only on PCL but not on platforms.
ConnectionHolder: Is the concrete implementation of IConnectionHolder interface and it has all logic needed at each platform. This class in injected on the Dependency Service of Xamarin.Form.
WebSocketRequest: This class is a concrete implementation of the interface IRequest, SignalR make a http negotiation before use any transport methodology and this object is the one used to establish the connection via HTTP for negotiation or for others transport method like Long Polling for example.
WebSocketTransportLayer: This class is a concrete implementation of the interface IClientTransport which is the interface used by the SignalR client to make the connections using the best transport methods the clients supports.
The second step was creating the websocket functionalities on each platform implementing the Interface that will be injected to abstract the platform behavior on the PCL.
WebSocketImplementation: This object is implemented on each platform IOS and Android and it implement the IWebSocket interface and is injected on the Dependency Service on Xamarin.Form like this [assembly: Xamarin.Forms.Dependency(typeof(WebSocketImplementation))].
Now you could use the websocket implementation with Xamarin.Form And SignalR.
Here is the source code: https://github.com/edgarleonardo/ChatAppXamarinFormWithWebSocket
Hope this help someone,
Thanks To These Sources I could make this article and the examples:
Regards,
This comment has been removed by the author.
ReplyDelete