Why I am getting this badmatch error when trying to connect to websocket

I am trying to use a JAVA library JavaPhoenixChannels to connect to Phoenix websocket. I get this error:

[error] Ranch listener MyAppWeb.Endpoint.HTTP had connection process started with :cowboy_clear:start_link/4 at #PID<0.13689.0> exit with reason: {{:badmatch, %{"event" => "phx_join", "join_ref" => "8", "payload" => %{}, "ref" => "8", "topic" => "client:me"}}, [{Phoenix.Socket.V2.JSONSerializer, :decode!, 2, [file: 'lib/phoenix/socket/serializers/v2_json_serializer.ex', line: 30]}, {Phoenix.Socket, :__in__, 2, [file: 'lib/phoenix/socket.ex', line: 564]}, {Phoenix.Endpoint.Cowboy2Handler, :websocket_handle, 2, [file: 'lib/phoenix/endpoint/cowboy2_handler.ex', line: 120]}, {:cowboy_websocket, :handler_call, 6, [file: '/srv/phx/memoapp/deps/cowboy/src/cowboy_websocket.erl', line: 482]}, {:cowboy_http, :loop, 1, [file: '/srv/phx/memoapp/deps/cowboy/src/cowboy_http.erl', line: 231]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}]}

What I am doing wrong here?

The obvious answer here is that your call was returning a map %{"event" => "phx_join", "join_ref" => "8", "payload" => %{}, "ref" => "8", "topic" => "client:me"} which did not map the expected return value in your code. I don’t know if this helps but without some code it is difficult to say more, especially as my knowledge if Phoenix is extremely limited. Extremely limited!

1 Like

I do not use this library, but I found this issue on it: https://github.com/eoinsha/JavaPhoenixChannels/issues/56

The v2 serializer expects all of the data to be in a JSON array. However, the envelope uses a JSON serializer that makes it look like a map https://github.com/eoinsha/JavaPhoenixChannels/blob/master/src/main/java/org/phoenixframework/channels/Envelope.java#L11 (I don’t really know much about Jackson).

Now this PR https://github.com/eoinsha/JavaPhoenixChannels/pull/60/files implies that v2 serializer support was implemented.

Here’s what I can’t figure out. It looks to me like the code still puts the data as a map instead of an array. This would imply that it doesn’t work with v2 serializer. I did a bit of spelunking and can’t quite resolve whether this is true or I’m missing something. However, the vast number of open issues makes it seem like it never quite worked with v2? You could try setting your version to 1.0.0.

1 Like

I am using this library now, works fine.

1 Like