Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
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
Mastering DART

You're reading from   Mastering DART Master the art of programming high-performance applications with Dart

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher Packt
ISBN-13 9781783989560
Length 346 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Sergey Akopkokhyants Sergey Akopkokhyants
Author Profile Icon Sergey Akopkokhyants
Sergey Akopkokhyants
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Beyond Dart's Basics 2. Advanced Techniques and Reflection FREE CHAPTER 3. Object Creation 4. Asynchronous Programming 5. The Stream Framework 6. The Collection Framework 7. Dart and JavaScript Interoperation 8. Internalization and Localization 9. Client-to-server Communication 10. Advanced Storage 11. Supporting Other HTML5 Features 12. Security Aspects Index

Why you should use streams


Just imagine that you need to provide a file on a server in response to a client's request. The following code will do this:

import 'dart:io';

main() {
  HttpServer
  .bind(InternetAddress.ANY_IP_V4, 8080)
  .then((server) {
    server.listen((HttpRequest request) {
      new File('data.txt').readAsString()
      .then((String contents) {
        request.response.write(contents);
        request.response.close();
      });
    });
  });
}

In the preceding code, we read the entire file and buffered it into the memory of every request before sending the result to the clients. This code works perfectly for small files, but what will happen if the data.txt file is very large? The program will consume a lot of memory because it serves a lot of users concurrently, especially on slow connections. One big disadvantage of this code is that we have to wait for an entire file to be buffered in memory before the content can be submitted to the clients.

The HttpServer object...

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