Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using the right networking APIs for your Universal Windows app

Similar presentations


Presentation on theme: "Using the right networking APIs for your Universal Windows app"— Presentation transcript:

1 Using the right networking APIs for your Universal Windows app
Sidharth Nabar, Himadri Sarkar Windows Networking team

2 Considerations when writing a networking app
Server-side support Scenario requirements Foreground vs background Portability requirements Performance considerations Ease of coding Which API should you use?

3 Discuss 4 networking scenarios &
In this talk Discuss 4 networking scenarios & API Choices for each HTTPS with user authentication Device-Cloud bi-directional communication Network communication between devices over sockets Download/Upload content to cloud in the background

4 HTTPS with User Authentication
Scenario 1 HTTPS with User Authentication

5 Recommended API: HttpClient
Features Client Authentication Username/Password or certificate? UI or Programmatic? Server Authentication Well-known or private CA? Custom validation/Certificate pinning? Recommended API: HttpClient

6 API Choices Windows.Web.Http & System.Net.Http √ √ (new!) Feature
Programming Language support All UWP languages C#/VB Private CA/Self-signed certificate Custom validation of server certificate √ (new!) X (on roadmap) Integration with native UI for Auth X Client Certificate Support Partial Cross-platform support (Xamarin)

7 Programming Pattern – Windows.Web.Http
1. Create an HttpBaseProtocolFilter instance (optional) var filter = new HttpBaseProtocolFilter(); 2. Modify settings on the filter (optional) filter.ClientCertificate = myCertificate; 3. Create an HttpClient instance var client = new HttpClient(); OR var client = new HttpClient(filter); 4. Send requests from client HttpResponseMessage response = await client.GetAsync(uri);

8 Demo: Online Auction App
Requirements: Custom server certificate validation Client Certificate API Choice: Windows.Web.Http.HttpClient

9 Summary: HTTPS with User Authentication
Windows.Web.Http Supports wide range of HTTP security and authentication features Available for all UWP programming languages System.Net.Http Supports programmatic use of credentials and certificates Available cross-platform for C#/VB developers (For more info and code snippets, see

10 Device-Cloud Bidirectional Communication
Scenario 2 Device-Cloud Bidirectional Communication

11 Features Communication Model Server Endpoint
Truly bidirectional or client-initiated? Half-duplex or full-duplex? Server Endpoint REST API or simple message exchange? Supports WebSockets? Performance Requirements/Optimizations Caching/Data Compression required? Latency/delay requirements?

12 API Choices Windows.Web.Http.HttpClient&
Windows.Networking.Sockets.WebSockets Feature HttpClient WebSockets Messaging pattern Request-response Bidirectional Duplex communication? Half Full Latency/overhead Higher Lower Proxy/Firewall traversal Server certificate custom validation √(new!) √ (new!) REST support, CRUD semantics Built-in Needs additional code Caching and data compression

13 API Choices (Contd.) MessageWebSocket & StreamWebSocket √ Feature
Message Format Discrete WebSocket messages Continuous data stream UTF-8 Strings/JSON data X Binary data Recommended data size Small (bytes/KB) Large (MB) Recommended content types Strings, JSON content Audio, Video, Photos

14 Programming Pattern – MessageWebSocket
1. Create an instance of WebSocket var socket= new MessageWebSocket(); 2. Register MessageReceived event handler socket.MessageReceived = myReceivingHandler; 3. Register Closed event handler socket.Closed = myClosedHandler; 4. Connect to server and send data. Close connection with status code at the end. await socket.ConnectAsync(serverUri); // Send data. socket.Close(code, reason);

15 Demo: Online Auction App
Requirements: Client-Server bidirectional communication Server relays others’ bids independent of client’s bids Minimum latency and overhead API Choice: Windows.Networking.Sockets.MessageWebSocket

16 Network Traffic Comparison
Bytes on the wire Wireshark trace for HttpClient run Wireshark trace for WebSocket run

17 Summary: Device-Cloud bidirectional communication
HttpClient Ideal for request-response communication Built-in support for REST API and message handling semantics WebSockets Ideal for low latency bidirectional communication Message format and semantics need to be implemented on top

18 Network communication between devices over sockets
Scenario 3 Network communication between devices over sockets

19 Features Lifespan of the socket Language Choice Pre-existing code
Foreground only or Background also Language Choice C#/VB, JavaScript or C++ Pre-existing code Win32 or .NET libraries

20 API Choices Windows.Networking.Sockets, System.Net.Sockets and Winsock
Feature Windows.Networking.Sockets System.Net.Sockets WinSock Language Support C++/CX X JavaScript C#/VB Activity Support in Background

21 Sockets – Listening in Background
Special trigger to start a background task – SocketActivityTrigger App can transfer ownership of socket to the OS while suspending OS keeps the socket alive even when the app is not running

22 Programming Pattern – Sockets in Background
1. Create a task with trigger type SocketActivityTrigger socketTaskBuilder.TaskEntryPoint = "SocketActivityBackgroundTask.SocketActivity Task"; var trigger = new SocketActivityTrigger(); socketTaskBuilder.SetTrigger(trigger); var task = socketTaskBuilder.Register(); 2. Associate Socket with the task and connect to the remote device socket.EnableTransferOwnership(task.TaskId, SocketActivityConnectedStandbyAction.Wake); await socket.ConnectAsync(target, port);

23 Programming Pattern – Sockets in Background
3. Transfer the ownership when suspending foreground app or Background Task cancellation socket.TransferOwnership(socketId); Things to note: At any point ownership of the socket can be only in one component Find the list of sockets owned by the service using: SocketActivityInformation.AllSockets Socket not in the list means socket is already closed or retrieved

24 Demo: Multi-player Game
Requirements: App communication between devices over sockets Listening on the socket activity even when the app is not running API Choice: Windows.Networking.Sockets

25 Listening on Socket in background
Summary Sockets APIs in UWP Recommended: Windows.Networking.Sockets Also available: System.Net.Socket, Winsock Listening on Socket in background Use SocketActivityTrigger

26 Download/Upload content to cloud in the background
Scenario 4 Download/Upload content to cloud in the background

27 Features Protocol Destination location Content Size
HTTP/ FTP Destination location Memory/File Content Size Small (in KBs)/Big (in MBs) Resiliency to reboot and network outage Resource Awareness Battery/Network cost

28 Windows.Networking.BackgroundTransfer
API Choices HttpClient (in background task) & BackgroundTransfer Feature HttpClient in BG Task Windows.Networking.BackgroundTransfer Protocol HTTP HTTP/FTP Destination Location In memory & File File only Recommended file size Small (KBs) Large (MBs) Resiliency (network connection drop/reboot) Needs additional code Can be resumed where left off Network Cost Awareness Built-in Battery Saver Awareness

29 Programming Pattern – Windows.Networking.BackgroundTransfer
1. Create an DownloadOperation instance BackgroundDownloader downloader = new BackgroundDownloader(); DownloadOperation download = downloader.CreateDownload(new Uri(downloadlocation), file); Task<DownloadOperation> startTask = download.StartAsync().AsTask(); 2. Start the transfer downloads = await BackgroundDownloader.GetCurrentDownloadsAsync (); await download.AttachAsync().AsTask(cts.Token, progressCallback); 3. Retrieve and attach to the transfers

30 Programming Pattern – Windows.Networking.BackgroundTransfer
4.Handling of Completion in foreground app Task<DownloadOperation> startTask = download.StartAsync().AsTask(); Task continueTask = startTask.ContinueWith(OnDownloadCompleted); 5.Handling of Completion using a background task BackgroundTransferCompletionGroup completionGroup = new BackgroundTransferCompletionGroup(); //Create background task builder builder.SetTrigger(completionGroup.Trigger); BackgroundDownloader downloader = new BackgroundDownloader(completionGroup);

31 Demo: Multi-player Game
Requirements: Download a new game level in the Background Show a toast notification on completion of the download API Choice: Windows.Networking.BackgroundTransfer

32 Summary – Download/Upload content in Background
BackgroundTransfer APIs in UWP Recommended for large files For smaller size/in-memory content - HTTPClient in background task Automatic completion handling Use trigger in BackgroundTransferCompletionGroup

33 References and More Info
MSDN: GitHub Universal Samples: HttpClient WebSockets SocketActivityStreamSocket BackgroundTransfer API Feedback:

34 Thank You!


Download ppt "Using the right networking APIs for your Universal Windows app"

Similar presentations


Ads by Google