SignalR Programming in Microsoft ASP.NET (英文版)


Professional José M. Aguilar SignalR Programming in Microsoft ASP.NET PUBLISHED BY Microsoft Press A Division of Microsoft Corporation One Microsoft Way Redmond, Washington 98052-6399 Copyright © 2014 by Krasis Consulting S.L. All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. Library of Congress Control Number: 2014930486 ISBN: 978-0-7356-8388-4 Printed and bound in the United States of America. First Printing Microsoft Press books are available through booksellers and distributors worldwide. If you need support related to this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think of this book at http://www.microsoft.com/learning/booksurvey. Microsoft and the trademarks listed at http://www.microsoft.com/about/legal/en/us/IntellectualProperty /Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property of their respective owners. The example companies, organizations, products, domain names, email addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, email address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Acquisitions Editor: Devon Musgrave Developmental Editor: Devon Musgrave Project Editor: Carol Dillingham Editorial Production: nSight, Inc. Technical Reviewer: Todd Meister; Technical Review services provided by Content Master, a member of CM Group, Ltd. Copyeditor: Richard Carey Indexer: Lucie Haskins Cover: Twist Creative • Seattle and Joel Panchot To my parents, for all the love and unconditional support you gave that kid who only liked computers. And to my three girls, Inma, Inmita, and María, for putting up with me daily and yet being able to give me so much love and happiness. —José M. Aguilar Contents at a Glance Introduction xiii CHAPTER 1 Internet, asynchrony, multiuser…wow! 1 CHAPTER 2 HTTP: You are the client, and you are the boss 5 CHAPTER 3 Introducing SignalR 17 CHAPTER 4 Persistent connections 27 CHAPTER 5 Hubs 57 CHAPTER 6 Persistent connections and hubs from other threads 103 CHAPTER 7 Real-time multiplatform applications 117 CHAPTER 8 Deploying and scaling SignalR 151 CHAPTER 9 Advanced topics 181 Index 233 vii Contents Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Chapter 1 Internet, asynchrony, multiuser…wow! 1 Chapter 2 HTTP: You are the client, and you are the boss 5 HTTP operations. . . . . . . . . . . . . . . . . . . . . . . . . .5 Polling: The answer?. . . . . . . . . . . . . . . . . . . . . . . . 7 Push: The server takes the initiative. . . . . . . . . . . . . . . . . .8 WebSockets. . . . . . . . . . . . . . . . . . . . . . . . . 9 Server-Sent Events (API Event Source). . . . . . . . . . . . . 11 Push today. . . . . . . . . . . . . . . . . . . . . . . . . 12 The world needs more than just push. . . . . . . . . . . . . 15 Chapter 3 Introducing SignalR 17 What does SignalR offer? . . . . . . . . . . . . . . . . . . . . . 18 Two levels of abstraction. . . . . . . . . . . . . . . . . . . . . .19 Supported platforms. . . . . . . . . . . . . . . . . . . . . . . 20 OWIN and Katana: The new kids on the block. . . . . . . . . . . . 21 Installing SignalR. . . . . . . . . . . . . . . . . . . . . . . . . 25 Chapter 4 Persistent connections 27 Implementation on the server side. . . . . . . . . . . . . . . . . 28 Mapping and configuring persistent connections. . . . . . . . 28 Events of a persistent connection. . . . . . . . . . . . . . . 30 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: microsoft.com/learning/booksurvey viii Contents Sending messages to clients . . . . . . . . . . . . . . . . . 32 Asynchronous event processing. . . . . . . . . . . . . . . .34 Connection groups. . . . . . . . . . . . . . . . . . . . . 35 The OWIN startup class. . . . . . . . . . . . . . . . . . . .37 Implementation on the client side. . . . . . . . . . . . . . . . . .38 Initiating the connection by using the JavaScript client. . . . . .38 Support for older browsers. . . . . . . . . . . . . . . . . .41 Support for cross-domain connections. . . . . . . . . . . . .41 Sending messages. . . . . . . . . . . . . . . . . . . . . .43 Receiving messages. . . . . . . . . . . . . . . . . . . . . 45 Sending additional information to the server. . . . . . . . . . 46 Other events available at the client . . . . . . . . . . . . . . 47 Transport negotiation . . . . . . . . . . . . . . . . . . . . . . .48 Adjusting SignalR configuration parameters. . . . . . . . . . . . . 50 Complete example: Tracking visitors. . . . . . . . . . . . . . . . .51 Project creation and setup. . . . . . . . . . . . . . . . . . 52 Implementation on the client side. . . . . . . . . . . . . . .53 Implementation on the server side. . . . . . . . . . . . . . .54 Chapter 5 Hubs 57 Server implementation . . . . . . . . . . . . . . . . . . . . . . 58 Hub registration and configuration. . . . . . . . . . . . . . 58 Creating hubs. . . . . . . . . . . . . . . . . . . . . . . .59 Receiving messages. . . . . . . . . . . . . . . . . . . . . 60 Sending messages to clients . . . . . . . . . . . . . . . . . 64 Sending messages to specific users. . . . . . . . . . . . . . 68 State maintenance. . . . . . . . . . . . . . . . . . . . . .69 Accessing information about the request context. . . . . . . . 71 Notification of connections and disconnections. . . . . . . . .72 Managing groups. . . . . . . . . . . . . . . . . . . . . . 72 Maintaining state at the server. . . . . . . . . . . . . . . . 73 Client implementation. . . . . . . . . . . . . . . . . . . . . . .78 Contents ix JavaScript clients. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79 Generating the proxy. . . . . . . . . . . . . . . . . . . . 79 Manual generation of JavaScript proxies. . . . . . . . . . . . 81 Establishing the connection. . . . . . . . . . . . . . . . . .83 Sending messages to the server. . . . . . . . . . . . . . . .86 Sending additional information. . . . . . . . . . . . . . . . 89 Receiving messages sent from the server. . . . . . . . . . . .90 Logging. . . . . . . . . . . . . . . . . . . . . . . . . . 91 State maintenance. . . . . . . . . . . . . . . . . . . . . .92 Implementing the client without a proxy. . . . . . . . . . . .93 Complete example: Shared drawing board. . . . . . . . . . . . . .96 Project creation and setup. . . . . . . . . . . . . . . . . . 97 Implementation on the client side. . . . . . . . . . . . . . .98 Implementation on the server side. . . . . . . . . . . . . . 100 Chapter 6 Persistent connections and hubs from other threads 103 Access from other threads. . . . . . . . . . . . . . . . . . . . .103 External access using persistent connections. . . . . . . . . .105 Complete example: Monitoring connections at the server. . . .106 Project creation and setup. . . . . . . . . . . . . . . . . .107 Implementing the website. . . . . . . . . . . . . . . . . .108 System for tracing requests (server side). . . . . . . . . . . .109 System for tracing requests (client side). . . . . . . . . . . . 111 External access using hubs. . . . . . . . . . . . . . . . . . . . 111 Complete example: Progress bar. . . . . . . . . . . . . . . 113 Project creation and setup. . . . . . . . . . . . . . . . . .113 Implementation on the client side. . . . . . . . . . . . . . 114 Implementation on the server side. . . . . . . . . . . . . . 115 Chapter 7 Real-time multiplatform applications 117 Multiplatform SignalR servers. . . . . . . . . . . . . . . . . . . 117 SignalR hosting in non-web applications. . . . . . . . . . . . . . . . . . . . . .118 SignalR hosting in platforms other than Windows . . . . . . . 126 x Contents Multiplatform SignalR clients. . . . . . . . . . . . . . . . . . . 129 Accessing services from .NET non-web clients. . . . . . . . . 130 Consumption of services from other platforms. . . . . . . . .149 Chapter 8 Deploying and scaling SignalR 151 Growing pains. . . . . . . . . . . . . . . . . . . . . . . . . .152 Scalability in SignalR. . . . . . . . . . . . . . . . . . . . . . . 155 Scaling on backplanes. . . . . . . . . . . . . . . . . . . . . . 159 Windows Azure Service Bus. . . . . . . . . . . . . . . . . 159 SQL Server. . . . . . . . . . . . . . . . . . . . . . . . .165 Redis. . . . . . . . . . . . . . . . . . . . . . . . . . . 167 Custom backplanes. . . . . . . . . . . . . . . . . . . . . . . .170 Improving performance in SignalR services. . . . . . . . . . . . . 173 Server configuration. . . . . . . . . . . . . . . . . . . . 174 Monitoring performance. . . . . . . . . . . . . . . . . . 175 Chapter 9 Advanced topics 181 Authorization in SignalR. . . . . . . . . . . . . . . . . . . . . 181 Access control in persistent connections. . . . . . . . . . . .181 Access control in hubs. . . . . . . . . . . . . . . . . . . .182 Client authentication. . . . . . . . . . . . . . . . . . . . 184 An extensible framework. . . . . . . . . . . . . . . . . . . . . 191 Dependency injection. . . . . . . . . . . . . . . . . . . . . . 196 Manual dependency injection. . . . . . . . . . . . . . . . 198 Releasing dependencies. . . . . . . . . . . . . . . . . . .200 Inversion of Control containers. . . . . . . . . . . . . . . .200 Unit testing with SignalR. . . . . . . . . . . . . . . . . . . . . 205 Unit testing of hubs. . . . . . . . . . . . . . . . . . . . .211 Unit testing persistent connections. . . . . . . . . . . . . .215 Intercepting messages in hubs. . . . . . . . . . . . . . . . . . .218 Integration with other frameworks. . . . . . . . . . . . . . . . .223 Web API. . . . . . . . . . . . . . . . . . . . . . . . . .223 Contents xi What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: microsoft.com/learning/booksurvey ASP.NET MVC. . . . . . . . . . . . . . . . . . . . . . . 226 Knockout. . . . . . . . . . . . . . . . . . . . . . . . . 227 AngularJS. . . . . . . . . . . . . . . . . . . . . . . . . 230 Index 233 xiii Introduction SignalR, Microsoft’s latest addition to the web development technology stack, is a framework that facilitates the creation of amazing real-time applications, such as online collaboration tools, multiuser games, and live information services, whose devel- opment has traditionally been quite complex. This book provides a complete walkthrough of SignalR development from scratch, but it will also deal with more advanced topics. The idea is that after reading it you will be familiar with the possibilities of this framework and able to apply it successfully in practice in the creation of real time systems of any size. It can also be used as a refer- ence manual because, although not exhaustively, it includes most features of practical application in the development of SignalR systems, and it provides the bases for fully mastering them. Who should read this book The aim of this book is to help developers understand, know, and program SignalR- based components or applications. It can be of special interest to developers who need to make use of real-time immediacy in existing applications or who want to create new systems based on this paradigm. Developers specializing in the back end will learn to implement real-time services that can be consumed from any client and to address scenarios such as those requiring scalability or quality improvement via unit tests. Those who are more oriented to the front end will see how they can consume real-time services and add spectacular fea- tures to their creations on the client side. Web developers, especially, will find a really simple way to break the limitations characteristic of the HTTP-based world, thanks to the use of push and the asynchrony of these solutions. Assumptions In this book, we will assume that the reader has a good knowledge of C# and program- ming within the .NET environment in general. Also, because SignalR itself and many of the examples and contents are focused on the web world, it is necessary to know the protocols on which it rests, as well as having a certain knowledge of the basic lan- guages of these environments, such as HTML and, in particular, JavaScript. xiv Introduction Although not strictly necessary, the reader might benefit from some prior knowl- edge about development with jQuery, Windows Phone 8, or WinRT for the chapters that develop examples and contents related to them. Familiarity with techniques such as unit testing, mocking, and dependency injection to get the most out of the final chapters could also prove helpful. Who should not read this book Readers who do not know the .NET platform and C# will not be able to benefit from this book. If you do not have prior knowledge of JavaScript, it will be difficult to follow the book’s explanations. Organization of this book This book is structured into nine chapters, throughout which we will go over differ- ent aspects of the development of real-time multiuser systems with SignalR, starting from scratch and all the way up to the implementation of advanced features of this framework. Chapter 1, “Internet, asynchrony, multiuser…wow!“ and Chapter 2, “HTTP: You are the client, and you are the boss,” are purely introductory, and they will help you understand the technological context and the foundations on which this new framework rests. In Chapter 3, “Introducing SignalR,” we will present SignalR at a high level, show- ing its position in the Microsoft web development technology stack and other related concepts such as OWIN and Katana. From this point, we will begin to look in detail at how to develop applications by using SignalR. We will dedicate Chapter 4, “Persistent connections,” and Chapter 5, “Hubs,” to study development from different levels of abstraction, using persistent con- nections and hubs. In Chapter 6, “Persistent connections and hubs from other threads,” we will study how to integrate these components with other technologies within the same application, and in Chapter 7, “Real-time multiplatform applications,” we will see how to implement multiplatform clients. Chapter 8, “Deploying and scaling SignalR,” will show different deployment sce- narios and the scaling solutions offered by SignalR. In Chapter 9, “Advanced topics,” we will find miscellanea where we will deal with more advanced aspects, such as security, extensibility, testing, and others. Introduction xv Finding your best starting point in this book Although this book is organized in such a way that it can be read from beginning to end following a path of increasing depth in the contents addressed, it can also be used as a reference by directly looking up specific chapters, depending on the level of knowledge the reader starts with and their individual needs. Thus, for developers who are approaching SignalR for the first time, the recommen- dation would be to read the book from beginning to end, in the order that the chap- ters have been written. However, for those who are acquainted with SignalR and have already developed with it in any of its versions, it will suffice to take a quick look at the first three chapters and then to pay closer attention to the ones dedicated to develop- ment with persistent connections or hubs to find out aspects they did not know about or changes from previous versions. From there, it would be possible to go directly to resolving doubts in specific areas, such as the scalability features of the framework, implementing authorization mechanisms, or the procedure for performing unit tests on hubs. In any case, regardless of the chapter or section, it is a good idea to download and install the related example projects, which will allow practicing and consolidating the concepts addressed. Conventions and features in this book This book presents information using the following conventions designed to make the information readable and easy to follow: ■■ Boxed elements with labels such as “Note” provide additional information or alternative methods for successfully completing a task. ■■ Text that you type (apart from code blocks) appears in bold. ■■ A plus sign (+) between two key names means that you must press those keys at the same time. For example, “Press Alt+Tab” means that you have to hold down the Alt key while you press the Tab key. ■■ A vertical bar between two or more menu items (for example, “File | Close”) means that you should select the first menu or menu item, then the next one, and so on. xvi Introduction System requirements To be able to adequately follow the examples shown in this book and practice with them, it is necessary to have, at least, the following hardware and software items: ■■ A computer equipped with a processor whose speed is at least 1.6 GHz (2 GHz recommended). ■■ 2 GB RAM (4 GB is advisable). ■■ A video card compatible with DirectX 9, capable of resolutions above 1024x768. ■■ The operating systems Windows 7 SP1, Windows 8, Windows 8.1, or Windows Server editions above 2008 R2 SP1. ■■ Internet Explorer 10. ■■ Visual Studio 2012 or above, in any of its editions. It is possible to use Express versions in most cases. ■■ An Internet connection. Some examples might require that you have a system account with administrator permissions or that you install complements such as the Windows Phone SDK. In some chapters, external resources are also used, such as Windows Azure services. Code samples Throughout this book you can find examples, and even complete projects, to illustrate the concepts dealt with. The majority of these, as well as other additional examples, can be downloaded from the following address: http://aka.ms/SignalRProg/files Follow the instructions to download the SignalRProgramming_codesamples.zip file. Note  In addition to the code samples, your system should have Visual Studio 2012 or 2013 installed. Introduction xvii Notes on the version This book has been written using version 2.0.0 of SignalR, so throughout it you will find various references to that specific version. However, the SignalR team at Microsoft is constantly striving to improve its product, so it frequently issues software updates. The numbering of these versions is usually of the 2.0.x or 2.x.0 type. Besides corrections, these updates might include some new or improved features, but not breaking changes or significant modifications of the devel- opment APIs. In any case, the contents of the book will still be valid after updating components to these new versions, although it will obviously be necessary to modify the existing refer- ences in the source code of the examples, especially in the case of references to script libraries. Thus, if we have a code such as the following: after installing version 2.0.1 of SignalR, it should be changed to this: Installing the code samples To install the code samples, just download the file indicated and decompress it into a folder in your system. Using the code samples After decompressing the file, a folder structure will have been created. The folders are organized in the same order as the chapters in the book, starting with Chapter 4, which is where we will begin to look at examples of code: … Chapter 04 – Persistent connections Chapter 05 – Hubs Chapter 06 – External access … xviii Introduction Inside each of these folders you can find a subfolder for each sample project included. These subfolders are numbered in the order that the concepts are dealt with in the book: … Chapter 08 – Scaling 1-AzureServiceBus 2-SqlServer … Inside these folders you can find the specific solution file (*.sln) for each example. The solutions are completely independent of each other and include a fully func- tional example that is ready to be run (F5 from Visual Studio), although in some cases it will be necessary to make some prior adjustments in configurations. In such cases, detailed instructions are always given for this on the main page of the project or in a readme.­txt file. Acknowledgments As trite as it might sound, a book such as this would not be possible without the col- laboration of many people who have helped with their time and effort for it to become a reality, and it is only fair to dedicate them a special word of thanks. In particular, I would like to thank my editor at campusMVP.net, Jose M. Alarcón (on Twitter at @jm_alarcon) for his involvement, his ability in the project management, coordination, and revision, as well as for his sound advice, all of which have led us here. Javier Suárez Ruíz’s (@jsuarezruiz) collaboration has also been essential, for his con- tributions and SignalR client implementation examples in non-web environments such as Windows Phone or WinRT. I would like to thank Victor Vallejo, of campusMVP.net, for his invaluable help with the text. On the part of Microsoft, I want to give thanks to the acquisitions editor, Devon Musgrave, for his interest in this project from the start, without which this book would have never been made. I also want to thank project editor Carol Dillingham for her expert work. Thanks go out to technical reviewer Todd Meister, copy editor Richard Carey, project manager Sarah Vostok of nSight, and indexer Lucie Haskins. And thanks to Sarah Hake and Jenna Boyd of O’Reilly Media for their support. Introduction xix Lastly, I would like to thank Damian Edwards and David Fowler for their invaluable input. It is a privilege to have been able to benefit from the suggestions and contribu- tions of the creators of SignalR to make this book as useful as possible. Errata & book support We have made every effort to ensure the accuracy of this book and its companion con- tent. Any errors that have been reported since this book was published are listed at: http://aka.ms/SignalRProg/errata If you find an error that is not already listed, you can report it to us through the same page. If you need additional support, email Microsoft Press Book Support at mspinput@ microsoft.com. Please note that product support for Microsoft software is not offered through the addresses above. We want to hear from you At Microsoft Press, your satisfaction is our top priority, and your feedback our most valuable asset. Please tell us what you think of this book at: http://aka.ms/tellpress The survey is short, and we read every one of your comments and ideas. Thanks in advance for your input! Stay in touch Let’s keep the conversation going! We’re on Twitter: http://twitter.com/MicrosoftPress. 1 CHAPTER 1 Internet, asynchrony, multiuser… wow! An application that combines Internet, asynchrony, and multiple users cooperating and interacting at the same time always deserves a “wow!”. At some point, we have all doubtlessly been amazed by the interactivity that some modern web systems can offer, such as Facebook, Twitter, Gmail, Google Docs, Office Web Apps, or many others, where we receive updates almost in real time without having to reload the page. For example, when we are editing a document online using Office Web Apps and another user also accesses it, we can see that they have entered the document and follow the changes that they are making. Even in a more everyday scenario such as a simple web chat, the messages being typed by our friend just appear, as if by magic. Both systems use the same type of solution: asynchronous data transfer between the server and the clients in real time. We developers who have had some experience in the world of the web are accustomed to the traditional approach proposed by the protocols that govern this environment—that is, the client is the active agent who makes requests asking for information, and the server merely answers. This is a probable reason for why we are so excited at the prospect of applications where the client side is directly updated by the server—for example, due to a new user having entered to edit the document or because our chat room buddy has written a new message in the chat. Pure magic. The world is undoubtedly demanding this immediacy: users need to know right away what is hap- pening in their environment, the documents they are working on, their social networks, their online games, and an increasing number of areas of their daily life. Instead of having to seek information as they used to do just a few years ago, now they want the information to come to them as soon as it is generated. These needs have been evident at web protocol level for some time, because the long-standing HTTP, as defined in its day, cannot meet them efficiently. In fact, the organizations that define web standards and protocols, and also browser developers, are aware of this and have been working for years on new mechanisms for communication between the client and the server in opposite direction to the conventional one—that is, allowing the server to take the initiative in communications. This has materialized into new protocols that can be used with a degree of reliability, although they are still rather far from universal solutions. The great diversity of client and server platforms, 2 Chapter 1  Internet, asynchrony, multiuser…wow! and even of network infrastructures, makes the adoption of these new mechanisms difficult and slow. Later on, we will delve in detail into these aspects. However, these are not the only issues that have to be addressed when developing real-time multiuser applications. Communications, as we know, constitute unstable and unpredictable variables, which make management and distribution of messages to users quite complicated. For example, in a chat room application, we could have users with very different bandwidths connected to the same room, and those bandwidths might even fluctuate throughout the chat session. To prevent the loss of messages in this scenario, the server should be capable of storing them temporarily, sending them to their recipients, and monitoring which users have received them already, always taking into account the conditions of communication with each user and the potential breakdowns that might occur dur- ing delivery, sending data again if necessary. In a sense, this is very similar to the features that we can find in traditional SMTP servers, but with the added requirement of the immediacy needed by real- time systems. It is easy to picture the complexity and difficulty associated with the implementation of a system such as the one described. Until recently, there was no component or framework in the area of .NET technologies provided by Microsoft that was capable of providing a complete solution to the problems of implementing this type of application. Certainly, there are many technologies capable of offering connected and disconnected services, such as the familiar Web Services, WCF, or the more recent Web API. However, none of them was specifically designed for asynchronous environments with real-time collaboration between multiple users. In fact, although it was possible to create this type of system with such plat- forms, it was not a trivial task even for the most experienced developers, and it frequently produced very inefficient systems with many performance problems and limited scalability. Throughout these pages, we will learn how to implement impressive features of this kind using SignalR, a remarkable framework—powerful, flexible, and scalable—which will facilitate our task to the point of making it trivial. For this purpose, we will first present a brief review of the problems that we find when developing real-time multiuser applications, some of which we have already mentioned. We will quickly look at HTTP operation and its limitations for supporting these types of systems, and we will introduce the push concept. We will also describe the standards that are currently in the process of being defined by W3C and IETF, as well as techniques that we can currently use for implementing push on HTTP. This will allow us to achieve a deep understanding of the scenario in which we are working and the chal- lenges surrounding the development of applications boasting the immediacy and interactivity that we have described. In turn, this will help us gain a better understanding of how SignalR works and the basis on which it rests. Next, we will formally introduce SignalR, describing its main features, its position within the stack of Microsoft technologies for web development, and the different levels of abstraction that it allows over the underlying protocols and which will help us remain separated from lower-level details so that we can just focus on creating spectacular features for our users. We will also take this chance to speak about OWIN and Katana, two new agents which are becoming increasingly prominent in various technologies, SignalR included. Internet, asynchrony, multiuser…wow!  Chapter 1 3 We will study in depth the various techniques and abstractions provided by this framework to cre- ate interactive multiuser real-time applications, both on the client and server sides, and we will learn to make use of their power and flexibility. Naturally, for this we will provide different code examples that will help us understand its basis in a practical way and thus illustrate how we can use this frame- work in real-life projects. We will also describe how SignalR is independent of web environments: although they might seem to constitute its natural environment, this framework goes far beyond them, allowing the provision of real-time services from any type of application and, likewise, their consumption from practically any type of system. We will see several examples of this. Another aspect of great importance, to which we will devote several pages, is reviewing the deployment and scalability of SignalR applications. We will study the “out-of-the-box” tools that come with this platform and point to other possible solutions when addressing scenarios where such tools are not powerful enough. Additionally, we will look at different techniques designed to monitor the status of our servers and improve their performance in high-concurrency environments. Finally, we will go into advanced aspects of programming with SignalR, which will give us deeper insight as to how the framework works, including security, creating decoupled components using dependency injection, SignalR extensibility, unit testing, and other aspects of interest. Welcome to multiuser real-time asynchronous applications. Welcome to SignalR! 5 CHAPTER 2 HTTP: You are the client, and you are the boss HTTP (HyperText Transfer Protocol) is the “language” in which the client and the server of a web application speak to each other. It was initially defined in 19961, and the simplicity and versatility of its design are, to an extent, responsible for the success and expansion of the web and the Internet as a whole. Although it is still valid in traditional web scenarios, there are others, such as real-time applications or services, for which it is quite limited. HTTP operations An HTTP operation is based on a request-response schema, which is always started by the client. This procedure is often referred to as the pull model: When a client needs to access a resource hosted by a server, it purposely initiates a connection to it and requests the desired information using the “language” defined by the HTTP protocol. The server processes this request, returns the resource that was asked for (which can be the contents of an existing file or the result of running a process), and the connection is instantly closed. If the client needs to obtain a new resource, the process starts again from the beginning: a con- nection to the server is opened, the request for the resource is sent, the server processes it, it returns the result, and then the connection is closed. This happens every time we access a webpage, images, or other resources that are downloaded by the browser, to name a few examples. As you can guess by looking at Figure 2-1, it is a synchronous process: after sending the request to the server, the client is left to wait, doing nothing until the response is available. 1 Specification of HTTP 1.0: http://www.w3.org/Protocols/HTTP/1.0/spec.html 6 Chapter 2  HTTP: You are the client, and you are the boss FIGURE 2-1  HTTP communication between a browser and a web server. Although this operation is a classic in web systems, the HTTP protocol itself can support the needs for asynchrony of modern applications, owing to the techniques generally known as AJAX (Asynchronous JavaScript And XML). Using AJAX techniques, the exchange of information between the client and the server can be done without leaving the current page. At any given moment, as shown in Figure 2-2, the client can initiate a connection to the server by using JavaScript, request a resource, and process it (for example, updating part of the page). What is truly advantageous and has contributed to the emergence of very dynamic and interactive services, such as Facebook or Gmail, is that these operations are carried out asynchronously—that is, the user can keep using the system while the latter communicates with the server in the background to send or receive information. FIGURE 2-2  AJAX in a webpage. This operating schema continues to use and abide by the HTTP protocol and the client-driven request-response model. The client is always the one to take the initiative, deciding when to connect to the server. However, there are scenarios in which HTTP is not very efficient. With this protocol, it is not easy to implement instant-messaging applications or chat rooms, collaboration tools, multiuser online games, or real-time information services, even when using asynchrony. HTTP: You are the client, and you are the boss  Chapter 2 7 The reason is simple: HTTP is not oriented to real time. There are other protocols, such as the pop- ular IRC2, which are indeed focused on achieving swifter communication to offer more dynamic and interactive services than the ones we can obtain using pull. In those, the server can take the initiative and send information to the client at any time, without waiting for the client to request it expressly. Polling: The answer? As web developers, when we face a scenario in which we need the server to be the one sending information to the client on its own initiative, the first solution that intuitively comes to our minds is to use the technique known as polling. Polling basically consists in making periodic connections from the client to check whether there is any relevant update at the server, as shown in Figure 2-3. FIGURE 2-3  Polling in a chat room service. The main advantages of this solution are, first, its easy implementation and, second, its universal application: it works in every case, with all browsers and with all servers, because it does nothing more than use the standard features of HTTP. And, of course, we still use the pull model. However, sometimes the price of polling is too high. Constant connections and disconnections have a high cost in terms of bandwidth and processing at both ends of communication. The worst part is that this cost increases proportionally to our need for faster updates and the number of clients making use of the service at a given time. In an application providing real-time updates, it is easy to imagine the load that a server has to bear when it has thousands of users connected, requesting several updates per second. 2 Internet Relay Chat (IRC) protocol: http://www.ietf.org/rfc/rfc1459.txt 8 Chapter 2  HTTP: You are the client, and you are the boss There are techniques to mitigate these problems insofar as possible. One of them is to use adap- tive intervals so that the interval between queries regularly adapts to the current system load or to the probability of new updates. This solution is quite easy to implement and can significantly improve resource consumption in some scenarios. There is a more conservative variant of polling, but it degrades user experience. It is the technique called piggy backing, which consists in not making deliberate queries from the client and, instead, ­taking advantage of any interaction between the user and the system to update any necessary infor- mation. To illustrate this, consider a web mail service: instead of making periodic queries to check for the arrival of new messages, those checks would be performed each time the user accessed a page, an email, or any other feature. This can be useful in scenarios that do not require great immediacy and in which the features of the system itself mean that we can be sure that the user will interact with the application frequently. Of course, these variants can be combined with each other to achieve more efficient usage of resources, offering at the same time a reasonable user experience. For example, to obtain the updates, it would be possible to update the status of a client via piggy backing when the client interacts with the server, using polling with or without adaptive periodicity when there is no such interaction. In conclusion, polling is a reasonable option despite its disadvantages when we want a solution that is easy to implement and that can be used universally and in scenarios in which a very high update frequency is not required. In fact, it is used a lot in current systems. A real-life example of its application is found in the web version of Twitter, where polling is used to update the timeline every 30 seconds. Push: The server takes the initiative We have already said that there are applications where the use of pull is not very efficient. Among them, we can name instant-messaging systems, real-time collaboration toolsets, multiuser online games, information broadcasting services, and any kind of system where it is necessary to send infor- mation to the client right when it is generated. For such applications, we need the server to take the initiative and be capable of sending informa- tion to the client exactly when a relevant event occurs, instead of waiting for the client to request it. This is precisely the idea behind the push, or server push, concept. This name does not make refer- ence to a component, a technology, or a protocol: it is a concept, a communication model between the client and the server where the latter is the one taking the initiative in communications. This concept is not new. There are indeed protocols that are push in concept, such as IRC, the protocol that rules the operation of classic chat room services, or SMTP, the protocol in charge of coordinating email sending. These were created before the term that identifies this type of communi- cation was coined. HTTP: You are the client, and you are the boss  Chapter 2 9 For the server to be able to notify events in real time to a set of clients interested in receiving them, the ideal situation would be to have the ability to initiate a direct point-to-point connection with them. For example, a chat room server would keep a list with the IP addresses of the connected clients and open a socket type connection to each of them to inform them of the arrival of a new message. However, that is technically impossible. For security reasons, it is not normally possible to make a direct connection to a client computer due to the existence of multiple intermediate levels that would reject it, such as firewalls, routes, or proxies. For this reason, the customary practice is for clients to be the ones to initiate connections and not vice versa. To circumvent this issue and manage to obtain a similar effect, certain techniques emerged that were based on active elements embedded in webpages (Java applets, Flash, Silverlight apps, and so on). These components normally used sockets to open a persistent connection to the server—that is, a connection that would stay open for as long as the client was connected to the service, listening for anything that the server had to notify. When events occurred that were relevant to the client con- nected, the server would use this open channel to send the updates in real time. Although this approach has been used in many push solutions, it is tending to disappear. Active components embedded in pages are being eliminated from the web at a dramatic speed and are being substituted for more modern, reliable, and universal alternatives such as HTML5. Furthermore, long-term persistent connections based on pure sockets are problematic when there are intermediary elements (firewalls, proxies, and so on) that can block these communications or close the connections after a period of inactivity. They can also pose security risks to servers. Given the need for reliable solutions to cover these types of scenarios, both W3C and IETF—the main organizations promoting and defining protocols, languages, and standards for the Internet— began to work on two standards that would allow a more direct and fluent communication from the server to the client. They are known as WebSockets and Server-Sent Events, and they both come under the umbrella of the HTML5 “commercial name.” WebSockets The WebSockets standard consists of a development API, which is being defined by the W3C (World Wide Web Consortium, http://www.w3.org), and a communication protocol, on which the IETF (Internet Engineering Task Force, http://www.ietf.org) has been working. Basically, it allows the establishment of a persistent connection that the client will initiate whenever necessary and which will remain open. A two-way channel between the client and the server is thus created, where either can send information to the other end at any time, as shown in Figure 2-4. 10 Chapter 2  HTTP: You are the client, and you are the boss FIGURE 2-4  Operation of the WebSockets standard. Although at the moment the specifications of both the API and the protocol are quite far advanced, we cannot yet consider this technology to be universally applicable. We can find implementations of WebSockets in many current browsers, such as Internet Explorer 10, Internet Explorer 11, Chrome, and Firefox. Some feature only partial implementations (Opera mini, Android browser), and in others, WebSockets is simply not available3. Aside from the problem of the different implementation levels at the client side, the fact that the standard includes an independent protocol for communication (although initially negotiated on HTTP) means that changes also have to be made on some infrastructural elements, and even on serv- ers, so that connections using WebSockets are accepted. For example, it has not been possible to use WebSockets easily on Microsoft technologies up until the very latest wave of developments (Internet Explorer 10, ASP.NET 4.5, WCF, IIS 8, and so on), in which it has begun to be supported natively. From the perspective of a developer, WebSockets offers a JavaScript API that is really simple and intuitive to initiate connections, send messages, and close the connections when they are not needed anymore, as well as events to capture the messages received: var ws = new WebSocket("ws://localhost:9998/echo"); ws.onopen = function() { // Web Socket is connected, send data using send() ws.send("Message to send"); alert("Message is sent..."); }; ws.onmessage = function(evt) { var received_msg = evt.data; alert("Message is received..."); }; ws.onclose = function () { // WebSocket is closed. alert("Connection is closed..."); }; 3 Source: http://caniuse.com/WebSockets HTTP: You are the client, and you are the boss  Chapter 2 11 As you can see, the connection is opened simply by instantiating a WebSockets object pointing to the URL of the service endpoint. The URL uses the ws:// protocol to indicate that it is a WebSockets connection. You can also see how easily we can capture the events produced when we succeed in opening the connection, data are received, or the connection is closed. Without a doubt, WebSockets is the technology of the future for implementing push services in real time. Server-Sent Events (API Event Source) Server-Sent Events, also known as API Event Source, is the second standard on which the W3 consor- tium has been working. Currently, this standard is in candidate recommendation state. But this time, because it is a relatively straightforward JavaScript API and no changes are required on underlying protocols, its implementation and adoption are simpler than in the case of the WebSockets standard. In contrast with the latter, Server-Sent Events proposes the creation of a one-directional chan- nel from the server to the client, but opened by the client. That is, the client “subscribes” to an event source available at the server and receives notifications when data are sent through the channel, as illustrated in Figure 2-5. FIGURE 2-5  Operation of the Server-Sent Events standard. All communication is performed on HTTP. The only difference with respect to a more traditional connection is the use of the content-type text/event-stream in the response, which indicates that the connection is to be kept open because it will be used to send a continuous stream of events—or ­messages—from the server. Implementation at the client is even simpler than the one we saw earlier for WebSockets: var source = new EventSource(‘/getevents’); source.onmessage = function(event) { alert(event.data); }; 12 Chapter 2  HTTP: You are the client, and you are the boss As you can guess, instantiating the EventSource object initiates the subscription of the client to the service whose URL is provided in the constructor, and the messages will be processed in the call- back function specified to that effect. Currently, almost all browsers support this standard except for Internet Explorer and some mobile- specific browsers, and this limits its use in real applications. Also, if we look at it from an infrastruc- tural point of view, we find that although being based on HTTP greatly simplifies its generalization, it requires the aid of proxies or other types of intermediaries, which must be capable of interpreting the content-type used and not processing the connections in the same way as the traditional ones—for example, avoiding buffering responses or disconnections due to time-out. It is also important to highlight the limitations imposed by the fact that the channel established for this protocol is one-directional from the server to the client: if the client needs to send data to the server, it must do so via a different connection, usually another HTTP request, which involves, for example, having greater resource consumption than if WebSockets were used in this same scenario. Push today As we have seen, standards and browsers are both getting prepared to solve the classic push ­scenarios, although we currently do not have enough security to use them universally. Nevertheless, push is something that we need right now. Users demand ever more interactive, agile, and collaborative applications. To develop them, we must make use of techniques allowing us to achieve the immediacy of push but taking into account current limitations in browsers and infra- structure. At the moment, we can obtain that only by making use of the advantages of HTTP and its prevalence. Given these premises, it is easy to find multiple conceptual proposals on the Internet, such as Comet, HTTP push, reverse AJAX, AJAX push, and so on, each describing solutions (sometimes coin- ciding) to achieve the goals desired. In the same way, we can find different specific techniques that describe how to implement push on HTTP more or less efficiently, such as long polling, XHR stream- ing, or forever frame. We will now study two of them, long polling and forever frame, for two main reasons. First, because they are the most universal ones (they work in all types of client and server systems), and second, because they are used natively by SignalR, as we shall see later on. Thus we will move toward the objectives of this book. Long polling This push technique is quite similar to polling, which we already described, but it introduces certain modifications to improve communication efficiency and immediacy. In this case, the client also polls for updates, but, unlike in polling, if there is no data pending to be received, the connection will not be closed automatically and initiated again later. In long polling, the connection remains open until the server has something to notify, as shown in Figure 2-6. HTTP: You are the client, and you are the boss  Chapter 2 13 FIGURE 2-6  Long polling. The connection, which is always initiated by the client, can be closed because of only two things: ■■ The server sends data to the client through the connection. ■■ A time-out error occurs due to lack of activity on the connection. In both cases, a new connection would be immediately established, which would again remain waiting for updates. This connection is used exclusively to receive data from the server, so if the client needs to send information upward, it will open an HTTP connection in parallel to be used exclusively for that purpose. The main advantage of long polling is the low delay in updating the client, because as soon as the server has data to update the state of the client, it will be sent through the channel that is already open, so the other end will receive it in real time. Also, because the number of connection openings and closures is reduced, resource optimization at both ends is much higher than with polling. Currently, this is a widely used solution due to its relatively simple implementation and the fact that it is completely universal. No browser-specific feature is used—just capabilities offered by HTTP. Resource consumption with long polling is somewhat higher than with other techniques where a connection is kept open. The reason is that there are still many connection openings and closures if the rate of updates is high, not forgetting the additional connection that has to be used when the cli- ent wants to send data to the server. Also, the time it takes to establish connections means that there might be some delay between notifications. These delays could become more evident if the server 14 Chapter 2  HTTP: You are the client, and you are the boss had to send a series of successive notifications to the client. Unless we implemented some kind of optimization, such as packaging several messages into one same HTTP response, each message would have to wait to be sent while the client received the previous message in the sequence, processed it, and reopened the channel to request a new update. Forever frame The other technique that we are going to look at is called forever frame and uses the HTML