How to re-listen to `ws.socket.on('message', function (message)...)` of onSignal in hc-web-client after calling `admin/interface/add_instance`

So I’ve been trying to figure out why the onSignal of hc-web-client stops getting called after I generate a new DNA instance. I figured out that this conductor admin call (admin/interface/add_instance) is a bit tricky to use. I’ve encountered two problems here.

  1. await on admin/interface/add_instance call just loads forever and this is probably because websocket connection is being refreshed (I was able to confirm that admin/interface/add_instance closes webscoket connection and opens it again) but I haven’t figured out why the refreshing of websocket connection makes this admin call load forever.
  2. In order to bypass the first problem, I did what @guillemcordoba also did in the holochain-provider (0.0.7-alpha.32) of uprtcl here and just setTimeout before caling the next conductor admin call. This allows me to call the next conductor admin call ('admin/instance/start') and I can get a resolved value back as well. However, the ws.socket.on('message', (message: any) => {} is not executing anymore. My guess is that when the admin/interface/add_instance call refreshed the ws connection, either hc-web-client wasn’t able to resubscribe to message OR holochain is not sending the message event from the hc side when a new instance is added to an existing interface already.

I’m failry sure that (admin/interface/add_instance) is the problem here since other conductor admin doesn’t cause the onSignal to stop executing. Right now, when I refresh my app, the onSignal will work fine again but I would really love for it to keep listening to the signal being sent from hc side without refreshing the app even if I called the (admin/interface/add_instance) call.

For more information, here’s the actual code and the admin/interface/add_instance is exactly being called here.

If anyone has encountered this problem and found a workaround, it would be amazing if you could share it!

Thank you so much!

2 Likes

Interested in the answer as well! Don’t have a fix at the moment, maybe @Connoropolous has got one?

1 Like

Actually, I think you just may have indicated a cause for a problem id seen in Acorn for a long time…

Looking again at hcwebclient, this code looks suspicious as the cause. It is ws.once … https://github.com/holochain/hc-web-client/blob/4eaa83318d47853ba2587b3d133703e8b3cb38d6/src/index.ts#L39

Prbly event listeners get removed on the closing of the socket, and since this is ws.once (fire this callback once and only once) instead of ws.on (everytime/anytime) it doesn’t reattach the relevant listener that calls onSignal

1 Like

Here @tats_sato

I haven’t tested this, I’m just workshopping code, but I think that’s right.

2 Likes

Hi @Connoropolous!!

Prbly event listeners get removed on the closing of the socket, and since this is ws.once (fire this callback once and only once) instead of ws.on (everytime/anytime) it doesn’t reattach the relevant listener that calls onSignal

Makes sense!

I haven’t tested this, I’m just workshopping code, but I think that’s right.

I tried your fix on my project and it definitely worked!!! :star_struck:
onSignal is definitely firing after close/reopen :smiley:
This is super awesome!! Thank you so much! :partying_face:
Will keep an eye on the PR and update our module once the PR is merged!

oh good, thanks for testing. In that case, I’ll make it a real pull request

2 Likes