Protobuffy Smaller Payloads in Elixir
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.