Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwarmClient re-connection to WebSocket #89

Open
aquiladev opened this issue Mar 23, 2019 · 6 comments
Open

SwarmClient re-connection to WebSocket #89

aquiladev opened this issue Mar 23, 2019 · 6 comments

Comments

@aquiladev
Copy link

I use @erebos/swarm-browser package in order to communicate with swarm in my app.

const client = new SwarmClient({ws: '...'});

When I restart swarm, erebos package throws an error Connection failed and it is not possible to continue work with app, in order to fix it I need to refresh the app.

Do you have any ideas how to implement automatic re-connection of SwarmClient

@PaulLeCam
Copy link
Collaborator

I'm guessing you're using the ws config in order to use PSS, so for a bit of context, the ws config is used by the @mainframe/rpc-ws-browser module, that itself is using the @mainframe/rpc-stream module over @mainframe/transport-ws-browser.

This transport is simply using RxJS' WebSocketSubject, that can be provided an openObserver and a closeObserver, so by creating the WebSocketSubject instance in your app, you could keep track of the connection state and reconnect accordingly.
I think the setup would look like:

import { SwarmClient } from '@erebos/swarm-browser'
import PssAPI from '@erebos/api-pss'
import StreamRPC from '@mainframe/rpc-stream'
import { WebSocketSubject } from 'rxjs'

const transport = new WebSocketSubject({
  // Config with url, openObserver and closeObserver
})
const rpc = new StreamRPC(transport)
const pss = new PssAPI(rpc)
const client = new SwarmClient({ pss })

Then it would be part of your application logic to recreate the client as needed.
I opened an issue in the relevant repository with some ideas regarding how this could be implemented as a separate package, and this article might provide some useful pointers.

@aquiladev
Copy link
Author

@PaulLeCam thanks for advice, I try it later today

@aquiladev
Copy link
Author

It took a while to implement it, coz it was a bit more complicated.

I didn't use PssAPI, coz it caused errors. I pas rpc directly in SwarmClient constructor.

I took reference implementation of WebSocketSubject wrapper https://gearheart.io/blog/auto-websocket-reconnection-with-rxjs-with-example
It works in both cases, when no connection and when connection lost.

Here is my implementation okamilab/nebula@881470f

@PaulLeCam
Copy link
Collaborator

Nice, thanks for sharing!

What errors did you get using PssAPI please? Any issue that should get fixed in Erebos?

@aquiladev
Copy link
Author

this.subject = new RxWebSocketSubject(ws);
const rpc = new StreamRPC(this.subject.socket);
const pss = new PssAPI(rpc);
this.client = new SwarmClient({ pss });

leads to an error on application startup

erebos.production.js:6105 
Uncaught Error: Invalid RPC instance provided: must support subscriptions
    at new t (erebos.production.js:6105)
    at _o (erebos.production.js:6635)
    at new t (erebos.production.js:6646)
    at new ClientResolver (client.js:14)
    at configureStore (redux.js:23)
    at Module../src/index.js (index.js:14)
    at __webpack_require__ (bootstrap:781)
    at fn (bootstrap:149)
    at Object.0 (reducer.js:22)
    at __webpack_require__ (bootstrap:781)
    at checkDeferredModules (bootstrap:45)
    at Array.webpackJsonpCallback [as push] (bootstrap:32)
    at main.chunk.js:1

@PaulLeCam
Copy link
Collaborator

Thanks for the details!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants