redux-websocket icon indicating copy to clipboard operation
redux-websocket copied to clipboard

Handle different types of WebSocket connections (such as Socket.io).

Open brianmcallister opened this issue 5 years ago • 3 comments

Right now, if you try to connect to a Socket.io backend, nothing will happen.

You can only pass ws: or wss: protocols to the WebSocket constructor (whereas Socket.io allows http: and https:, and will handle the upgrade for you.

If you do pass the underlying wss: protocol URL, the connection will open, but because the raw WebSocket connection doesn't know how to interact with the Socket.io backend, it will immediately close.

We should support at least Socket.io at the outset.

brianmcallister avatar Oct 04 '19 17:10 brianmcallister

So is there any support for Socket.io? @samuelcastro is referring to a test case https://github.com/giantmachines/redux-websocket/issues/11. But I cannot find it. Would love to help!

Ridder90 avatar Apr 06 '20 14:04 Ridder90

Hi @Ridder90, the library currently does not support socket io. It is on our wish list for the next major release. The test case mentioned in #11 was from an older version of the library that allowed for passing in your own instance of a raw websocket.

Feel free to create a fork and play around with adding socket io support. We'd appreciate any contributions you can make!

My initial thoughts are to add socket io as an optional dependency and allow for the library to take an instance of socket io somewhere in the options. I think it would also be nice to have an interface that abstracts away the different websocket implementations so we can easily support other libraries going forward.

interface WebsocketClient {
    connect(url: string);
    disconnect();
    send(_store: MiddlewareAPI, { payload }: Action);
    ...
}

class SocketIOClient extends WebsocketClient { ... }

class NativeWebsocketClient extends WebsocketClient { ... }

procchio6 avatar Apr 07 '20 13:04 procchio6

Yess! On second thought I concluded I do not really need the socket to communicate with the global store and implemented the socket directly in the component. But I think I will need it in the near future. So when I have time i will try to come up with something.

Ridder90 avatar Apr 13 '20 16:04 Ridder90