SignalR Real-time Application Cookbook

SignalR Real-time Application Cookbook

Roberto Vespa

Language: English

Pages: 292

ISBN: 1783285958

Format: PDF / Kindle (mobi) / ePub


Use SignalR to create realtime, bidirectional, and asynchronous applications based on standard web technologies

About This Book

  • Build high performance real-time web applications
  • Broadcast messages from the server to many clients simultaneously
  • Implement complex and reactive architectures

Who This Book Is For

Different levels of developers will find this book useful. Beginners will be able to learn all the fundamental concepts of SignalR, quickly becoming productive in a difficult arena. Experienced programmers will find in this book a handy and useful collection of ready-made solutions to common use cases, which they will be able to enhance as needed. Developers can also use the book as a quick reference to the most important SignalR features. No previous practical experience either with SignalR or with real-time communication in general is required.

What You Will Learn

  • Teach you how to build SignalR servers
  • Illustrate SignalR clients built with both the JavaScript and the .NET client libraries
  • Demonstrate both the Hubs API and the Persistent Connection API
  • Demystify the lifetime of a connection
  • Explain how to authorize requests
  • Scale up and scale out your SignalR applications
  • Enable you to handle errors efficiently
  • Extend SignalR with custom services
  • Solve complex real-time scenarios with the help of advanced, ready-made examples

In Detail

SignalR is a recent addition to ASP.NET, which allows you to add real-time functionalities to your applications. SignalR enables bidirectional communication between client and server over HTTP, transparently, and ensures you're always provided with the experience of a persistent connection.

SignalR Real-time Application Cookbook is a practical, hands-on guide that provides a number of clear step-by-step recipes that will gradually enable you to add SignalR as an innovative, effective, and useful item in your toolbox. This book will move from simple examples on to complex use cases, going through a comprehensive overview of the library. Other than helping out with the common tasks, at the end of the book you will find a set of ready-made solutions for more complex scenarios.

Lightweight Django

Client Centric Web Design

Ruby and MongoDB Web Development Beginner's Guide

The Art and Science of Web Design

The Truth About HTML5 (For Web Designers)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

using the Start() method. Its return value is a task-derived object, which means its execution will be asynchronous; indeed, we are not allowed to use a Hub until the connection process has properly completed. That's why in this example we use the Wait() method to block our code until the connection is effectively ready. When we are done with the Wait() call, we can safely address any method on our Hub proxy. We know that EchoHub exposes a Say() method, so we call it using Invoke, to which we

type, which we'll call EchoHub. For more details about these steps, you can check the previous chapters. Now, the following steps need to be performed to edit the code: 1. We first need to make the EchoHub code look similar to the following: using System; using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs; [HubName("echo")] public class EchoHub : Hub { public string Say(string message) { if (new Random().Next(2) == 0) throw new ApplicationException("Doh!"); return message; } }

selecting and applying a connection strategy as we already saw earlier in the book. If a specific client (for example, a browser window) loses its connection and is able to reconnect just after the reconnection retry timeout has expired, a new full-blown connection will be built, a new connectionId value will be assigned to it and OnConnect() will be called again. We need a client to test if everything is working properly. To achieve this goal, let's add an HTML page called index.html, which

project, we'll just need to reference the Microsoft.AspNet.SignalR.JS NuGet package using either the Manage NugGet Packages… project context menu or the Package Manager Console. 2. We then add an HTML page called index.html, linking the usual JavaScript files that have been added to the project with the previous step, as shown in the following code: 130 Chapter 6 3. We can now add a

server-side portion is ready. Let's quickly add an index.html page and add the client code to it. All the necessary references are already in place because they have been automatically added when we created our Hub. Add the following code to the index.html page: