Issue with Websockex and json encoding

I’m trying to send a message using websockex but I’m getting an error when I try to send out my json string (I used Jason), what could I possibly be missing?

"{\"action\":\"subscribe\",\"options\":{\"accounts\":[\"nano_1qzfp3op48im348qdybmrheu9dogtopj1jyioguq9pyo5i7mkqgo4jaswp4a\"],\"all_local_accounts\":true},\"topic\":\"confirmation\"}"
** (ArgumentError) argument error
    :erlang.send(MitoNode.Client, {:"$websockex_cast", "{\"action\":\"subscribe\",\"options\":{\"accounts\":[\"nano_1qzfp3op48im348qdybmrheu9dogtopj1jyioguq9pyo5i7mkqgo4jaswp4a\"],\"all_local_accounts\":true},\"topic\":\"confirmation\"}"})
    (websockex) lib/websockex/utils.ex:57: WebSockex.Utils.send/2
    (websockex) lib/websockex.ex:398: WebSockex.cast/2

:erlang.send/2 only fails when the first argument is an atom and this atom is not registered to a process name.

So you need to make sure, that there is actually a process with the name MitoNode.Client.

Or pass a pid that actually does exist.

But all of this is just guessing, as you have not even provided code, but just an error.

Sorry, my bad

Heres the construction of the message.

  def sub() do

    {:ok, message} = %{
    "action" => "subscribe",
    "topic" => "confirmation",
    "options" => %{
      "all_local_accounts" => true,
      "accounts" => [
        "nano_1qzfp3op48im348qdybmrheu9dogtopj1jyioguq9pyo5i7mkqgo4jaswp4a"
      ]
    }
  } |> Jason.encode

    IO.inspect(message)

    WebSockex.cast(__MODULE__, message)
  end

in my supervision tree I just have the child {MitoNode.Client, ["random WebSockex message"]} as per the example but I will try a pid too

That just delegates the start of the client to its start_link/1, IIRC.

But in that, do you actually name the started process?

1 Like

Man you’re the best. Forgot the opts field in start_link, which of course had a name: field, tried subbing with the atom name I gave it and it works fine now. Thanks for the quick advice!

You can also use the pid of the WebSockex process and WebSockex.send_frame/2 to send the data. e.g.

{:ok, pid} = MyWebSockexWrapper.start_link()


msg = Jason.encode!(%{
    "action" => "subscribe",
    "topic" => "confirmation",
    "options" => %{
      "all_local_accounts" => true,
      "accounts" => [
        "nano_1qzfp3op48im348qdybmrheu9dogtopj1jyioguq9pyo5i7mkqgo4jaswp4a"
      ]
    })

WebSockex.send_frame(pid, {:text, msg})