DEV Community

Mạnh Vũ
Mạnh Vũ

Posted on • Edited on

Replace JSON by TOON in Phoenix Channel

Intro

I'm build a realtime system based on Phoenix Channel. After some workload/benchmark test, I realized encode/decode task in JSON is quite expensive tasks for our system 🤔

I have replaced Jason (default Phoenix JSON lib) by native JSON and save tons of CPU and memory resource. I quite happy for this lib but still want to try more and look around.

One day, I found TOON format that can save traffic cost for our system.

TOON format

I found this is quite fun because I saw people use for saving tokens (and money) and improve accuracy for LLM.

The documents of TOON said can save 30-60% tokens, I found an interested thing in here, I can save bandwidth for network (and my server can serve more users). I also found some benchmarks say parsing in TOON is faster than JSON, that's nice 👌

Another interested thing is TOON is compatible with JSON, so that enough for me deep dive to TOON.

Look a way to apply Phoenix Channel

After look to Phoenix implement I see Phoenix is using JSON quite simple. A module for serializing between JSON and Elixir type. This module use other library. Change encoder/decoder just by set config in config.exs file.

config:

# used native JSON lib (built-in Erlang) for save cpu & memory
config :phoenix, :json_library, JSON
Enter fullscreen mode Exit fullscreen mode

After this, I went to find libs that support to encode/decode between string and Elixir type.

I asked on elixir forum and toon_ex is library the member in forum suggested for me, that's nice because toon_ex is quite similar to Jason library.

Now I can go further.

Apply TOON to Phoenix

Because TOON is compatible with JSON then I though I can replace JSON lib for both Phoenix LiveView and Phoenix Channel. For LiveView it need to integrate with JS in client then I chose to start with Channel first.

After some hours to try toon_ex I found too many bugs and missed a function encode_to_iodata for Phoenix, I decided improve this.

Now toon_ex can run smoothly, I go to integrate with Phoenix and build a client to test.

In client, I built a Websocket client for Phoenix Channel, I also use improved toon_ex library. It works perfectly.

I try with some complex types and this runs smoothly.

In my system, Phoenix Channel run in a different app then I don't need to update LiveView this time.

I published my PoC and my updated toon_ex at:

Conclusion

TOON format is still new and in Elixir ecosystem it's not good enough, I saw on hex.pm just has two libraries and these are not mature.

Phoenix quite easy framework for deep dive and see what happen. From my view the maintainers of the framework can use serializer as plug like other module then user can easy to use with other type than JSON (like MessagePack,...).

I will continue to try to add TOON for LiveView and update this topic later.

I add some features for TOON and published on hex.pm

Thanks for reading!

Top comments (0)