I am trying to send a cart information when a user join the channel. But having some trouble serializing a cart information.
in my cart_channel.ex
def join("cart:" <> user_id, _params, %{assigns: %{current_user: user}} = socket) do
# check if current user match client user
if String.to_integer(user_id) == user.id do
# If authorized, return cart information
{carts, total_items, total_price} = OrderManager.get_updated_carts(user.id)
IO.puts("Cart channel join successful")
socket =
assign(socket, :carts, carts)
|> assign(:total_items, total_items)
|> assign(:total_price, total_price)
send(self(), :send_cart)
{:ok, socket}
else
{:error, %{reason: "unauthenticated"}}
end
end
def join("cart:" <> _user_id, _params, _socket) do
{:error, %{reason: "unauthenticated"}}
end
def handle_info(
:send_cart,
socket = %{assigns: %{carts: carts, total_items: total_items, total_price: total_price}}
) do
push(socket, "cart", %{carts: carts, total_items: total_items, total_price: total_price})
{:noreply, socket}
end
and got an error like this
[error] GenServer #PID<0.1082.0> terminating
** (Protocol.UndefinedError) protocol Jason.Encoder not implemented for %MyApp.Checkout.Order{} of type MyApp.Checkout.Order (a struct), Jason.Encoder protocol must always be explicitly implemented.
How can I solve this?