Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.
11:19

Chrome supporting EventSource for server push, and richer Worker API

Rick Waldron has been delving into Chrome and Chromium to find some nice updates.

First, he uncovers new support for the EventSource API that allows for simple server push of DOM events as shown in this simple client and server pairing:

JAVASCRIPT:
document.addEventListener('DOMContentLoaded', function () {
 
  var eventSrc  = new EventSource('events.php');
 
  eventSrc.addEventListener('open', function (event) {
    console.log(event.type);
  });
 
  eventSrc.addEventListener('message', function (event) {
    console.log(event.type);
    console.log(event.data);
  });
}, false);
 
PHP:
<?php
header("Content-Type: text/event-stream\n\n");

// despite not having the while(true){}
// this seems to repeat pushing messages to the client
echo 'data: ' . time() . "\n";
 

Then, he finds out that the Web Worker API now supports complex messages, so you can postMessage more than Strings. Send over objects and Arrays with ease:

JAVASCRIPT:
//  TEST ANOTHER THREAD SAFE OBJECT ARG
var obj = new Object();

obj.isArray         = [ 1,2,3,4,5 ];
obj.isString        = 'Foo bar baz',
obj.resultOf        = (function () {
                        return 'returned from self executing function';
                      })();
worker.postMessage(obj);

//  TEST STRING ARG
worker.postMessage('Hello Worker Process');

//  TEST ARRAY ARG
worker.postMessage([ 1, 2, 3, 4 ]);

//  TEST BOOLEAN ARG
worker.postMessage(false);
 

(PRO)
No Soup for you

Don't be the product, buy the product!

close
YES, I want to SOUP ●UP for ...