[erlang-questions] BERT encoding and WebSocket

Erik Ylipää erik.ylipaa@REDACTED
Wed Apr 11 12:45:58 CEST 2012


Hello!

Binary frames are actually supported, at least by chrome (and should be
supported by any browser which implements at least hybi-07).

I got it working with cowboy and chrome by modifying
the websocket_handler:websocket_info/3 from the cowboy websocket example:

websocket_info(tick, Req, State) ->
{reply, {binary, term_to_binary("Hello")}, Req, State, hibernate};

The browser receives a Blob which can be decoded by a FileReader object
using readAsBinaryString followed by BERT-JS decode.

Sending from the browser is also not a problem. Converting the data from
BERT-JS encoding requires you to convert it to an ArrayBuffer or Blob which
introduces some extra steps, but it is doable.

Even though it's possible, I'd use JSON if the browser doesn't have to know
how the data is represented in Erlang. For the project I'm working on, we
came to the conclusion that if we used BERT, we had to do more work in
JavaScript. If we used JSON, we had to do more work in Erlang.

Doing more work in Erlang is preferable, so we chose to go with JSON :)

/Erik Ylipää

2012/4/11 Ngoc Dao <ngocdaothanh@REDACTED>

> Thanks for the info.
>
> About "websockets does not support binary data":
> * Wikipedia says "Binary frames are not supported yet in the API"
> ("http://en.wikipedia.org/wiki/WebSockets")
> * Stackoverflow says "until then, WebSockets payload is encoded as
> UTF-8" (
> http://stackoverflow.com/questions/5766802/send-and-receive-binary-data-over-web-sockets-in-javascript
> )
>
> About BERT, it seems that BERT is not shorter than JSON, so I'm
> thinking of not using BERT.
>
>
> On Tue, Apr 10, 2012 at 11:17 PM, Antoine Koener
> <antoine.koener@REDACTED> wrote:
> > Here's a link: http://autobahn.ws/testsuite/reports/clients/index.html
> >
> > :-)
> >
> > On Tue, Apr 10, 2012 at 3:54 PM, Antoine Koener
> > <antoine.koener@REDACTED> wrote:
> >> Are you sure that websockets does not support binary data ?
> >>
> >> The data type is "blob", or "arraybuffer".
> >>
> >> See w3c for more information.
> >>
> >> (sorry for not giving you Uris about that, I'm conferencing :))
> >>
> >> --
> >> Was in mobile mode ...
> >>
> >> On 10 avr. 2012, at 04:26, ngocdaothanh <ngocdaothanh@REDACTED> wrote:
> >>
> >>> What is the best way to use BERT encoding (http://bert-rpc.org/) with
> >>> WebSocket?
> >>>
> >>> BERT is binary but WebSocket does not support binary at this moment.
> >>> Should I use base64 to encode BERT binary to normal string? Base64 has
> >>> 33% overhead (http://en.wikipedia.org/wiki/Base64). How much space can
> >>> BERT save compared to JSON?
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120411/9df2b10c/attachment.htm>


More information about the erlang-questions mailing list