23

Is there a way to do protocol buffers in JavaScript?

Why for .js?

If you think about sciencey requirements for a moment, situations pop up where you might want to send a large block of data to the client. With CRUD-style it doesn't really matter so much what you use. With sciencey stuff it does matter (at least I think it does).

tradeoffs:

  • protobuffs balances compactness, serialize and deserialize speeds well.

  • text based protocols (xml / json) have a larger message size... but with javascript I wonder which is more effective.

reference:

Additional references provided by community (see below for more context):

4
  • ty. Added this as a reference. Commented Aug 16, 2011 at 6:25
  • 2
    I came across another Protocol Buffers for JavaScript project. It seems to be active and relies on Google's closure library: code.google.com/p/protobuf-plugin-closure Commented Aug 16, 2011 at 15:06
  • ty. There's some interest out there for this at least. I wonder sometimes why people don't combine forces more. Commented Aug 16, 2011 at 15:15
  • @AndrewBarber upon review, perhaps your objection was that the question opened in a chatty way. Since the question was first written, the JS community has evolved considerably. However, first-class protobuffer support is still needed in JavaScript. I think the question is still relevant. Commented Nov 21, 2013 at 5:33

3 Answers 3

14

Google makes heavy use of Protocol Buffers in JS (GMail, etc.) through their Closure Library, generating JS code with a (unfortunately non-open-sourced) modified protoc (it would probably have to be ported to a protoc extension before being open-sourced).

Apache Wave (whose client webapp is built with GWT) also uses Protocol Buffers for its communications with the server, generating Java code by reflecting on the Java classes produced by protoc (this is the PST, aka protobuf-stringtemplate, subproject).
Previously, Wave was using protostuff (and I don't know why they switched to their own solution, I suspect PST is derived from what the original Google Wave was using, and protostuff was only an intermediate step during the move to open-source).

As a side note, I started exploring using Protocol Buffers on the browser side a while ago: http://blog.ltgt.net/exploring-using-protobuf-in-the-browser/ & http://blog.ltgt.net/using-protobuf-client-side-with-gwt with some almost-working code at http://code.google.com/p/protobuf-gwt/ that you might want to resurrect.

Finally, there's work underway to make GWT RequestFactory proxies compatible with the server-side Java classes generated by protoc (and you could use a protoc extension or a similar approach to Wave's PST to generate your RequestFactory proxies). It should already be possible, provided you use builders all the way on the server-side (which is not quite how the Protocol Buffers Java API was designed).

Sign up to request clarification or add additional context in comments.

3 Comments

I started thinking about the cross browser issues for a JavaScript version of protobufs. Did you many issues with this in your testing?
We actually settled on a different architecture and aren't using Protocol Buffers at all, so there haven't been much testing per se. But most if not all of the cross-browser issues (related to encoding/decoding the messages to JSON and transporting your protobuf/json-encoded message (XMLHttpRequest)) would be taken care of by either Closure or GWT (or the JS lib of your choice).
sorry... switched from your informative answer to dcode's one. Looks like the state of js proto is changing.
9

Historically javascript made working with binary a pain, which probably in part explains a relative lack of tooling - but with javascript typed arrays it could well be a lot easier now. I kinda agree that if you have to get the same volume of data (via some format), using less bandwidth is a plus - but before embarking on anything you'd need to check that bandwidth / processing was an actual bottleneck (and if bandwidth: have you tried gzip/deflate first).

I'm a fan of protobuf - and I'd happily see stronger browser-side tooling for it, but json is so ubiquitous that you'd need a compelling reason to challenge the status-quo. Also; think "jsonp".

2 Comments

took a look at "jsonp". Nice technique. Thanks for the reminder on gzip... it seems a pity to introduce extra representations + steps.
@sgtz my point re jsonp is that jsonp solves a specific issue re same-origin; I'm not sure how you could address the same same-origin issue with raw data.
7

I have been looking for protobuf for javascript. There is a project here: https://github.com/dcodeIO/ProtoBuf.js

5 Comments

thanks. I'll keep an eye on this project. In future I'd like to support something like this. Fingers crossed that a capable library will emerge.
Nothing changed since the 0.0.0 release in 2008. Might still be okay, of course. Did you experiment with this?
Sorry, haven't worked on this on a while. Cannot answer much. From what I remember it did what it advertised. I don't have this in production so I cannot vouch for it with certainty.
This is the best js protobuf library I have found. Currently using it and it is working well.
Seems it's active recently (2014). With 11 releases, 21 open vs 127 closed issues, wiki seems not rich enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.