Protobuffy Smaller Payloads in Elixir

hassan shaikley
1 min readSep 27, 2024

Reduce the amount of data sent over the wire. Kind of protobuffy.

Sometimes 40 or 50% of the data you send is in the keys. So remove em. Use array indexes instead. Like imageine: {“userIsAGoatPersonFromMars”: true]. The key is so many bytes! All for a little boolean. It might make sense to get rid of the key in the transport phase.

This does mean more processing on the client and possibly on the server. Depends on your bottlenecks and concerns.

Not always the optimization you want. But definitely a neat thing to keep in mind at the very least.

Doesn’t need to be a socket thing, can be for http, or really any protocol where you have bandwidth concerns. But heres a socket example.

player_payload = “[#{player.id}, #{player.position_x}, #{player.position_z}, #{player.hp}]”
MyAppWeb.Endpoint.broadcast(“my:topic”, “update player”, {:binary, player_payload})

Then on the client:

const player_data_array = new Uint8Array(player_payload);
const playerData = String.fromCharCode(…player_data_array);
const updatedPlayer = JSON.parse(playerData);
// updatedPlayerArray[0] is is the id, [1] is the position.x

Bonus points, protect yourself against JSON highjacking attacks by prepending `)]}` to the payload. In this example it doesn’t matter. But it might be an issue for you depending on what you do.

payload = “)]}[#{player.id}, #{player.position_x}, #{player.position_z}, #{player.hp}]”

And remove it on the client.

const player_data_array = new Uint8Array(player_payload.slice(3));

It’s late, there’s a hurricane, and I am tired. I haven’t tested this fully but you get the idea.

I hope you learned something neat.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

hassan shaikley
hassan shaikley

No responses yet

Write a response