New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebSocket transport should allow to exchange binary message #52

Closed
flowersinthesand opened this Issue Jan 21, 2015 · 3 comments

Comments

Projects
None yet
1 participant
@flowersinthesand
Contributor

flowersinthesand commented Jan 21, 2015

Needs on binary message is obvious. For example, application may use binary event for live video streaming and text event for chat using one connection. How to enable exchanging binary message varies for each transport and is different with text message case. Of course, both text and binary message should be able to be exchanged together via one transport. (Each message is either text or binary. It doesn't mean a message may consist of text and binary together.)

  • WebSocket
    WebSocket protocol specifies binary data frame so no more work is needed.
  • HTTP
    In theory binary payload can be sent and received through HTTP protocol but practically these transports should be available on old browser. Such constraint limits transport protocol as usual.
    • Sending binary message from client to server
      It's common to streaming and long polling transport. In modern browser, XMLHttpRequest can handle binary payload and the server can recognize payload type from conent-type request header. In old browser, Base64 is the only option. The client and server may need a payload type flag determining whether this message is text or binary.
    • Sending binary message from server to client using streaming
      Even Server-Sent Event which is the best technology in browser for streaming isn't designed for binary-based data so Base64 should be used as that link recommends. In fact, it's possible to specify new message format to embrace both text and binary message and use it with XMLHttpRequest2. But it's hard work and must be slower than using EventSource with Base64 in my opinion because EventSource and Base64 are browser's native implementation. Also it is not available on old browsers.
    • Sending binary message from server to client using long polling
      For modern browser, it's enough to write content-type response header and binary message as response body. For old browser, Base64 is the only option. Of course, every time Base64 is used, a message type flag is needed.

For now there is no test suite only for transport and this feature will be tested properly once exchanging binary event through socket is allowed. But as you can see, this feature requires a lot of change in codebase especially to HTTP transports so with this feature, new release will be cut as intermediate release to cover binary feature.

@flowersinthesand

This comment has been minimized.

Show comment
Hide comment
@flowersinthesand

flowersinthesand Jan 23, 2015

Contributor

For now there is no test suite only for transport and this feature will be tested properly once exchanging binary event through socket is allowed.

But #53 is stuck on how it should be implemented, which is not easy to handle. At least to test this functionality on the fly, it should be possible to handle transport through API. Therefore #50 should be landed first.

Contributor

flowersinthesand commented Jan 23, 2015

For now there is no test suite only for transport and this feature will be tested properly once exchanging binary event through socket is allowed.

But #53 is stuck on how it should be implemented, which is not easy to handle. At least to test this functionality on the fly, it should be possible to handle transport through API. Therefore #50 should be landed first.

@flowersinthesand

This comment has been minimized.

Show comment
Hide comment
@flowersinthesand

flowersinthesand Jan 27, 2015

Contributor

I used the following scripts to test WebSocket transport and they are executable on Node console.

Server

var vibe = require("./lib");
var http = require("http");
var url = require("url");

var server = vibe.transport.createWebSocketServer().on("transport", function(transport) {
    console.log('opened');
    transport.on("text", function(message) {
        console.log("on text", message);
        transport.send(message);
    }).on("binary", function(message) {
        console.log("on binary", message, message.toString());
        transport.send(message);
    });
});

http.createServer().on("upgrade", function(req, sock, head) {
    if (url.parse(req.url).pathname === "/vibe") {
        server.handle(req, sock, head);
    }
}).listen(8080);

Client

var vibe = require("./lib");
var transport = vibe.transport.createWebSocketTransport("ws://localhost:8080/vibe");
transport.on("open", function() {
    console.log('opened');
    transport.send("hello");
    transport.send(new Buffer([104,101,108,108,111])); // equals to new byte[]{'h','e','l','l','o'}
}).on("text", function(message) {
    console.log("on text", message);
}).on("binary", function(message) {
    console.log("on binary", message, message.toString());
}).open();

Then server console log

> opened
on text hello
on binary <Buffer 68 65 6c 6c 6f> hello

and client console log

> opened
on text hello
on binary <Buffer 68 65 6c 6c 6f> hello
Contributor

flowersinthesand commented Jan 27, 2015

I used the following scripts to test WebSocket transport and they are executable on Node console.

Server

var vibe = require("./lib");
var http = require("http");
var url = require("url");

var server = vibe.transport.createWebSocketServer().on("transport", function(transport) {
    console.log('opened');
    transport.on("text", function(message) {
        console.log("on text", message);
        transport.send(message);
    }).on("binary", function(message) {
        console.log("on binary", message, message.toString());
        transport.send(message);
    });
});

http.createServer().on("upgrade", function(req, sock, head) {
    if (url.parse(req.url).pathname === "/vibe") {
        server.handle(req, sock, head);
    }
}).listen(8080);

Client

var vibe = require("./lib");
var transport = vibe.transport.createWebSocketTransport("ws://localhost:8080/vibe");
transport.on("open", function() {
    console.log('opened');
    transport.send("hello");
    transport.send(new Buffer([104,101,108,108,111])); // equals to new byte[]{'h','e','l','l','o'}
}).on("text", function(message) {
    console.log("on text", message);
}).on("binary", function(message) {
    console.log("on binary", message, message.toString());
}).open();

Then server console log

> opened
on text hello
on binary <Buffer 68 65 6c 6c 6f> hello

and client console log

> opened
on text hello
on binary <Buffer 68 65 6c 6c 6f> hello

@flowersinthesand flowersinthesand changed the title from Allow to exchange binary message through transport to WebSocket transport should allow to exchange binary message Jan 29, 2015

@flowersinthesand flowersinthesand added this to the 3.0.0-Alpha10 milestone Jan 29, 2015

@flowersinthesand

This comment has been minimized.

Show comment
Hide comment
@flowersinthesand

flowersinthesand Jan 29, 2015

Contributor

I'll separate this issue into WebSocket part and HTTP part. As WebSocket part is fixed, this issue is closed.

Contributor

flowersinthesand commented Jan 29, 2015

I'll separate this issue into WebSocket part and HTTP part. As WebSocket part is fixed, this issue is closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment