Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
SignalR Real-time Application Cookbook

You're reading from   SignalR Real-time Application Cookbook Use SignalR to create real-time, bidirectional, and asynchronous applications based on standard web technologies.

Arrow left icon
Product type Paperback
Published in Apr 2014
Publisher
ISBN-13 9781783285952
Length 292 pages
Edition Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Roberto Vespa Roberto Vespa
Author Profile Icon Roberto Vespa
Roberto Vespa
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

SignalR Real-time Application Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
1. Understanding the Basics FREE CHAPTER 2. Using Hubs 3. Using the JavaScript Hubs Client API 4. Using the .NET Hubs Client API 5. Using a Persistent Connection 6. Handling Connections 7. Analyzing Advanced Scenarios 8. Building Complex Applications Creating Web Projects Insights Index

Asynchronous programming and SignalR


This book has no ambition of being an architectural one, and it does not go too deep into technological issues because those would take a lot of space and distract the content from its direct goal of delivering straightforward solutions to common problems. Nevertheless, the fully asynchronous nature of SignalR deserves a couple of words and some reference material for those interested in digging into it.

The trend in programming in the last few years has been pretty clear: asynchronicity rules. Whether you are building a modern and reactive UI, a database-intensive backend, or a distributed system, you want to optimize resources, and the first logical choice to achieve that is avoid holding them if you don't use them. In other words, you never want any code to be stuck waiting for something else, because blocked code does nothing while still holding precious computational power that could be used for good in different ways.

This brings complexity, because it's clearly harder to know when a result is ready if we do not wait for it anymore. Computer scientists have been working on these problems for a long time, but it's just recently that mainstream platforms started supplying programming models more sophisticated than classical callbacks, which do their work fine but inevitably lead to intricate and unreadable code.

Nowadays, we have easy access to several better approaches that allow us to manage asynchronous code better. One of those is the idea of a promise, or future value. This is an object that represents a value that will be ready at some point in the future, and its availability will happen asynchronously with respect to the logical thread of code that has a reference on the promise itself. The great advantage of this model is the fact that promises can be composed, leading to more natural and readable programs that end up looking sequential even if they are totally asynchronous.

JavaScript does not support promises natively, but there are several libraries implementing the pattern. One of those is the jQuery Deferred Object, which we have mentioned here because it's the one used by SignalR. Another quite popular one is the Q library (https://github.com/kriskowal/q).

On the .NET side, the Task API, available since Version 4.0, is an implementation of the promise pattern that allows the usage of the Task-based Asynchronous Pattern (TAP), and the async/await syntax from C# 5.0 greatly simplifies the composition of promises. What's important here is the fact that most of the networking infrastructure available with .NET is fully asynchronous and implements TAP.

SignalR, on the one hand, leverages and embraces such support from the networking infrastructure, and on the other exposes itself the very same way, making sure things are done the most efficient way possible behind the scenes while clients are enforced to do the same by the model. On the server, methods are invoked when corresponding low-level messages become asynchronously available, resulting in a highly scalable messaging framework. On the client, the connection and hub's proxy objects expose methods that return composable promises, leading developers to write clean and efficient code.

This is an incredibly vast topic we just wanted to briefly introduce; you can find a lot of material about it on the Web for your investigations. The following are a few helpful links:

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image