question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Streamable apps

See original GitHub issue

Proposal: Stream-able apps

This proposal seeks to add streaming abilities to DoneJS, providing an improved user experience. By streaming, we mean we are able to write HTML out to the user as soon as we have the data for it. Instead of waiting for all the data to return from the database or a service call, we are able to stream records as they are retrieved into meaningful output to a user. Specifically, we seek to enable streaming during initial rendering (SSR) and also once the browser has loaded.

This should have a significant impact on performance, especially SSR performance. When rendering an HTML page on the server, we will be able to stream out the <head> immediately which typically includes <link> tags that load external resources. Getting that HTML to the browser immediately, means that the browser can start loading those resources while the database is still hypothetically returning data.

SSR flow

streaming-ssr

Overview of streaming flow

  1. Browser makes a request to the todos.html page.
  2. SSR server begins rendering an instance of the application.
  3. <head>{{title}}</head> and <link href=“theme.css”> are not waiting on anything asynchronous, so they are streamed to the HTML result.
  4. The <ul> (probably) isn’t waiting on any asynchronous behavior, so it is streamd to the result.
  5. The {{#each todosPromise.value}} does the following:
    1. Does a request for Todo.getList({}) which results in a promise that resolves immediately to a DefineList.
    2. This fires off a .fetch() request to /api/todos.ndjson whose results will be streamed to that DefineList.
    3. That DefineList is passed to can-view-live.list.
    4. The DefineList, can-view-live.list and the streaming DOM serializer are able to coordinate in such a way to notify the DOM serializer to wait.
  6. Services server handles the /api/todos.ndjson request.
  7. Services server gets a pooled connection and makes a SELECT query to the database.
  8. As records come back, the services server converts them into newline delimited JSON, and writes the records in chunks back to the SSR server.
  9. The SSR server parses out the new JSON records, .push() each record into the DefineList.
  10. By adding the record into the DefineList, a new <li> is created
  11. The streaming DOM serializer is able to identify this change, serialize the DOM of the <li> and res.write it to the browser.
  12. The browser displays the contents of the <li>.

Steps 8-12 repeat as records come back from the Database.

Once the database records have completed:

  1. The Services server response wil be ended res.end().
  2. The asynchronous behavior associated with the DefineList will complete.
  3. can-view-live.list will mark all of its nodes as being ready for serialization.
  4. The streaming DOM serializer will move onto the closing </ul>.
  5. The SSR server will write out the closing </ul> to the browser.

Things to build

  • - A ndjson stream transformer:
    fetch("todos.ndjson") .then( (response) => {
  
     return ndjsonStream( response.body  );
    
}).then( (todosStream) => {
  
});
    
  • - A streaming can-connect behavior:
    // get a streaming list of todos
    Todo.getList({}).then(function(todos){
      template({ todos: todos });
    });
    
    <!-- template updates as todos are returned -->
    <ul>
      {{#each todos}}
        <li>{{name}}</li>
      {{/each}}
    </ul>
    
  • - A streaming DOM serializer:
    var serialize = require('vdom-streaming-serializer');
    http.createServer(function(request, response){
    
      var document = makeDocument();
    
      // ... ADD APP TO DOCUMENT ...
      myApp(document);
    
      // send back HTML as it is "completed"
      var stream = serialize(document.documentElement);
      stream.pipe(response);
    
    });
    
  • - A notification mechanism for asynchronous and/or streaming behavior:
    var observationZone = require("can-observation-zone");
    
    // A stateful object
    var todos = new DefineList([
      {id: 1, name: "lawn"}
    ]);
    
    // Indicates the length is going to change in the future
    var updatedTodos = observationZone.add(todos, "length");
    
    var completed = observationZone.completed(todos,"length");
    completed.then(function(){
      console.log("zone is completed");
    })
    
    setTimeout(function(){
      todos.push({id: 2, name: "dishes"});
      updatedTodos();
      console.log("updatedTodos")
    },1);
    
    // logs
    //  - updatedTodos
    //  - zone is completed
    
  • - Streaming aware live-binding utilities
  • - A demo app that puts it all together
donejs-streaming-dev-server

Links

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
leoj3ncommented, Mar 1, 2017

That is so cool. Very elegant. I am going to have to play with this, and possibly rig up an objective speed comparison test.

0reactions
matthewpcommented, Oct 30, 2017

Going to close in favor of https://github.com/donejs/donejs/issues/787, of course we will being making future streaming enhancements in the future.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Livestream Marriage Proposal - Lovecast - Private Event ...
Share your private event website with unlimited guests, one-click is all they need to watch the proposal live stream. No app download or...
Read more >
The Proposal | Where to Stream and Watch - Decider
Looking to watch The Proposal? Find out where The Proposal is streaming, if The Proposal is on Netflix, and get news and updates,...
Read more >
Top 10 Best Streaming Apps - Reviews.org
We ranked the top 10 best streaming services by comparing overall content, originals, channels, user experience, and extra features.
Read more >
The Best Live Streaming Platforms For Your Virtual Wedding
Don't overlook the possibilities of live streaming your proposal, ... WedWed Mobile is a wedding app tool that not only live streams your ......
Read more >
How to Create Live Streaming App in 2022 - Uptech
Video on Demand Streaming​​ This type of live streaming app allows users to schedule and watch their favorite TV shows online as and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found