Hacker News new | ask | show | jobs
by rezoner 2574 days ago
If you don't have better ideas for improving your packet size at least push your JSON through msgpack. Using strings as keys (properties) is ultra bad for your bandwidth too. In general JSON is only suitable for turn based games.
4 comments

For my games[1] I use a custom binary protocol which is not that difficult to pull off. Actually building a buffer with JavaScript's DataView[2] and ArrayBuffers produces very compact results since you're free to optimize the packets for each specific game - for example a single uInt8 can store 8 different flags for a game character - things like dead/alive, up/down, right/left, walking/running, etc.

[1] https://redka.games [2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

If you don't have better ideas for improving your packet size at least push your JSON through msgpack.

I'm about to change over to msgpack for my own multiplayer real time browser game. I'd use protobuf, but the extra code generation step would get in the way of my plans to turn my MMO into an MMO-game-as-lambdas PaaS project.

Right now, I'm using my own protocol which uses something somewhat like base-85 for encoding, except it's base-92. It's a binary packing format, but packs directly into base-92 encoding. This packed base-92 is encapsulated into JSON, which minimizes the use of keys. This might all sound horrible, but I'm able to achieve what Flatpack achieves: no copies are needed to extract the data. That, and the remaining slight JSON overhead makes it easier to debug network traffic.

Thanks for pointing this out. This will be my next improvement.
ps: Don't use the official implementation - it is slow. Go with one of the lite projects like that one https://github.com/kawanet/msgpack-lite
Much appreciated!
Protobuf would be an excellent serialization. It's compact, binary, and efficient. There's a ton of library support in nearly every language imaginable.
I am not 100% sure, but I think I read somewhere (at least 2 years ago), that browsers are actually faster using JSON than Protobuf (a bit counter-intuitive).

Nevertheless, bandwidth-wise it could make sense anyway, but I think it makes sense to consult some benchmarks in this case.