kino icon indicating copy to clipboard operation
kino copied to clipboard

Serialize `Kino.JS` payloads upfront

Open jonatanklosko opened this issue 3 years ago • 1 comments

Currently payloads are serialized on the Livebook side (LivebookWeb.JSViewChannel), so structs from dependencies are not supported.

image

One way to address this is to serialize before sending messages to Livebook (as a new payload type). For serialization we need jason, but we don't want to depend on it to keep kino lightweight, so we can serialize upfront only when jason is already available.

For decimal specifically, we could add it as a dependency in Livebook, but I'm still not convinced on that :)

jonatanklosko avatar Aug 03 '22 22:08 jonatanklosko

For more context, the only reason we would add Decimal to Livebook is because Jason has an optional dependency on it, so using it gives us a UndefinedFunctionError for Decimal.to_string instead of the expected Protocol.UndefinedErrror. I guess an alternative is to also detected UndefinedFunctionError for Decimal.to_string and raise a good error message for it.

josevalim avatar Aug 04 '22 07:08 josevalim

Another idea is to try to encode to JSON and, if that fails, convert to_string on the client itself. Since JSON encoding is recursive, we may need additional features on Jason to make it work (like a per encoding :fallback_to_any callback).

josevalim avatar May 06 '23 10:05 josevalim

  • [x] Add Decimal as a dependency to Livebook (only to improve UX given Jason depends on it already)
  • [ ] Allow Kino to sent either encoded data (if Jason is available) or rich data (as today)
  • [x] Livebook should detect protocol undefined error and improve the error message

josevalim avatar Jul 26 '23 10:07 josevalim

Livebook depends on Decimal via Ecto now, and the error message is already specialised.

I can't think of cases where we would actually just pass random structs, so for now we are closing and we can revisit the optional Jason approach if a relevant use case comes up.

jonatanklosko avatar Jul 26 '23 21:07 jonatanklosko