Subscriptions-transport-ws: Error on SubscriptionClient connection

Created on 8 Dec 2017  路  2Comments  路  Source: apollographql/subscriptions-transport-ws

When I try to make a request

const ws = new SubscriptionClient('ws://192.168.100.6:4000/socket/websocket', {
  reconnect: true,
});

I get this error:

[error] Ranch protocol #PID<0.1252.0> (:cowboy_protocol) of listener Bark.Web.Endpoint.HTTP terminated
** (exit) exited in: Phoenix.Endpoint.CowboyWebSocket.resume()
    ** (EXIT) an exception was raised:
        ** (Phoenix.Socket.InvalidMessageError) missing key "topic"
            (phoenix) lib/phoenix/socket/message.ex:33: Phoenix.Socket.Message.from_map!/1
            (phoenix) lib/phoenix/transports/websocket.ex:121: Phoenix.Transports.WebSocket.ws_handle/3
            (phoenix) lib/phoenix/endpoint/cowboy_websocket.ex:77: Phoenix.Endpoint.CowboyWebSocket.websocket_handle/3
            (cowboy) /Users/yamildiazaguirre/Documents/grvty/Bark-Backend/deps/cowboy/src/cowboy_websocket.erl:588: :cowboy_websocket.handler_call/7
            (phoenix) lib/phoenix/endpoint/cowboy_websocket.ex:49: Phoenix.Endpoint.CowboyWebSocket.resume/3
            (cowboy) /Users/yamildiazaguirre/Documents/grvty/Bark-Backend/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

But when I try to connect to my WS with a wscat client, I can connect without any problem

$ wscat -c ws://192.168.100.6:4000/socket/websocket

connected (press CTRL+C to quit)

Most helpful comment

@Yamilquery
This library doesn't appear to be compatible with phoenix absinthe subscriptions, based on the message format built here: https://github.com/apollographql/subscriptions-transport-ws/blob/master/src/client.ts#L372

and the message format required by phoenix here:
https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/socket/message.ex#L5

any workarounds or other libraries you found?

All 2 comments

@Yamilquery
This library doesn't appear to be compatible with phoenix absinthe subscriptions, based on the message format built here: https://github.com/apollographql/subscriptions-transport-ws/blob/master/src/client.ts#L372

and the message format required by phoenix here:
https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/socket/message.ex#L5

any workarounds or other libraries you found?

I also encountered this error, and I was scared for a moment that nobody posted a solution yet. Though I found out that if you use Absinthe, apparently there are special JS libraries created by the Absinthe team to properly communicate with Phoenix Socket. See the guide at https://hexdocs.pm/absinthe/apollo.html#content Seems that you're supposed to use those libraries instead of subscriptions-transport-ws.

Was this page helpful?
0 / 5 - 0 ratings