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
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Mastering Node.js

You're reading from   Mastering Node.js Expert techniques for building fast servers and scalable, real-time network applications with minimal effort

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781782166320
Length 346 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Sandro Pasquali Sandro Pasquali
Author Profile Icon Sandro Pasquali
Sandro Pasquali
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Mastering Node.js
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
1. Understanding the Node Environment FREE CHAPTER 2. Understanding Asynchronous Event-Driven Programming 3. Streaming Data Across Nodes and Clients 4. Using Node to Access the Filesystem 5. Managing Many Simultaneous Client Connections 6. Creating Real-time Applications 7. Utilizing Multiple Processes 8. Scaling Your Application 9. Testing your Application Organizing Your Work Introducing the Path Framework Creating your own C++ Add-ons Index

Managing state


As we discussed earlier:

If changes can be introduced into a web application without requiring a complete reconstruction of state and state display then updating client information becomes cheaper. The client and server can talk more often, regularly exchanging information. Servers can recognize, remember, and respond immediately to client desires, aided by reactive interfaces gathering user actions and reflecting the impact of those actions within a UI in near real time.

A very important question every application developer must ask is this one: where should state reside?

Of course, permanent data must ultimately reside in a persistent layer such as a server-side database. As well, transient-state data (such as which navigation item is highlighted in a UI) can happily exist in the client. However, if an application displays real bank records in a data grid within a browser, how do changes made to the canonical record (the database) synchronize with changes in client model?

For example, imagine a banking application where a browser-based UI indicates to the user that she has a balance of USD 100. This UI was "drawn" based on an earlier request to a server, asking something like: What is Mary's bank balance? Clearly that number may change in the next second, or even millisecond, given the automated nature of banking. How can Mary be sure that when she pays a USD 100 bill she still has USD 100 in the bank?

One solution is to periodically expire the validity of the client state and poll a server for state changes, resynching client and server state. Additionally, client requests are revalidated on the server—Mary would be warned if she tries to spend USD 100 that she doesn't have. Client state is optimistic, and transactions are revalidated.

However, there are several problems with this model:

  • Not all state can exist on a client: Secured state data must remain on the server, which means clients can handle only pieces of state, making even the best attempt to maintain a synchronized client state imperfect by definition.

  • Revalidation is not an acceptable solution in many cases: Consider a real-time shooter. If one player alters the state of another player (let's say by killing that player), it is not acceptable to at some point in the future reanimate the killed player simply because the shooter's client had incorrect position information that renders the original kill invalid.

  • Optimism leads to false security: Meaningless is one word that can be used to described state data that may or may not be valid. For example, a stock-trading program showing the wrong price for FOO Corporation may lead a trader to short sell BAR Corporation. If the state of BAR (according to the client) matches that on a server the trade will go through, and the trader may lose money. Any single bit of information cannot itself validate all other bits of information. Indeterminacy is a feature of concurrent systems, and these systems challenge deductive state validation techniques—whether a comparison is valid cannot always be determined.

Many techniques have been used to synchronize client and server state—cookies, hidden form fields, fat URLs, and others. Path takes a different view, based on the advantages that Node provides, in particular when paired with the WebSocket protocol. If we can create highly responsive servers linked with clients through efficient pipelines, where sub-second response times are expected, it is better to simply keep important state information on the server and cease any attempts to create client-side reflections on a mirror that is easily smudged.

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 $15.99/month. Cancel anytime
Visually different images