HTTP streaming transport should allow to exchange binary message #53

Closed
flowersinthesand opened this Issue Feb 2, 2015 · 2 comments

Projects

None yet

1 participant

@flowersinthesand
Contributor

As explained in vibe-project/vibe-protocol#56, there are two approaches (A. Defining a new message format embracing both text and binary message. B. Using Binary-to-text encoding with the current format.) and both approaches can be applied to JavaScript client as we decided to drop supporting browser where XMLHttpRequest 2 and Typed Arrays are not available. However, I prefer B to A for the following reasons:

  • Easy to implement
    While it takes time to define a new message format and to implement it for A, it takes time to find a way to distinguish binary-to-text message and plain text message for B. As Base64 is most proper binary-to-text encoding in browser in terms of performance and availability, there is no need to think of which binary-to-text encoding should be used.
  • Performance
    First of all, for the same implementation, non-native implementation can't run faster than native implementation. For A, native XMLHttpRequest and non-native implementation to parse response body are used, but for B, native EventSource, atob and btoa are used though I'm not sure yet. Of course we need figure to compare them technically. But, at least in case of text message, it is clear that B is faster than A. FYI, EventSource is supported by every browsers supporting XMLHttpRequest 2 and Typed Arrays except IE.
  • Memory usage
    XMLHttpRequest keeps every chunk to aggregate them to the complete response body. That means with XMLHttpRequest if the server sends total 20MB payload, the browser will keep that payload in the memory until the connection is closed even though that payload has never stored to somewhere. On the other hand, EventSource keeps chunks only until a message is found. In this case, if the server sends total 20 MB payload (it's known that Base64 would incur high (33%) byte overhead so technically speaking it may be about 27 MB), the browser will keep nothing in the memory unless that payload has stored to somewhere.
@flowersinthesand flowersinthesand added this to the 3.0.0-Alpha13 milestone Feb 2, 2015
@flowersinthesand
Contributor

Implmenting B method is very simple than I thought

@flowersinthesand
Contributor

Test result along with vibe-project/vibe-protocol#56 (comment)

In browser,

var uri = "http://localhost:8080/vibe?transport=stream";
var transport = vibe.transport.createHttpStreamTransport(uri);
transport.on("open", function() {
    console.log('opened');
    transport.send("hello");
    transport.send(new Uint8Array([104,101,108,108,111]));
}).on("text", function(message) {
    console.log("on text", message);
}).on("binary", function(message) {
    console.log("on binary", message, new TextDecoder().decode(new DataView(message)));
}).open();

// opened
// on text hello
// on binary ArrayBuffer {} hello

In Node,

var vibe = require("./vibe");
var uri = "http://localhost:8080/vibe?transport=stream";
var transport = vibe.transport.createHttpStreamTransport(uri);
transport.on("open", function() {
    console.log('opened');
    transport.send("hello");
    transport.send(new Buffer([104,101,108,108,111]));
}).on("text", function(message) {
    console.log("on text", message);
}).on("binary", function(message) {
    console.log("on binary", message, message.toString());
}).open();

// opened
// on text hello
// on binary <Buffer 68 65 6c 6c 6f> hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment