post-me icon indicating copy to clipboard operation
post-me copied to clipboard

Extend the library to more use cases

Open alesgenova opened this issue 5 years ago • 7 comments

The utilities provided by post-me can be useful for any use case in which there is only one low level channel of communication (similar to postMessage).

By adding more classes implementing the Messenger interface (and possibly extending the Messenger interface if needed) we can directly support more scenarios.

Just a couple of example that come to mind:

  • Websockets
  • ~~MessageChannels~~ [#47 ]

alesgenova avatar Jan 19 '21 22:01 alesgenova

Audio Worklet support would be nice. It seemed to hang on the handshake when I tried it. Of course, I could've made a mistake

grahamlyus avatar Feb 10 '21 02:02 grahamlyus

@grahamlyus Yes, audio worklet would be really interesting!

Do you have a small repo/gist to provide with your attempts at using post-me with the worklet? Would be really helpful to get started

alesgenova avatar Feb 10 '21 03:02 alesgenova

@alesgenova I'll try to get an minimal example going this week.

grahamlyus avatar Feb 10 '21 17:02 grahamlyus

@alesgenova It works! I modified your demo repo: https://github.com/grahamlyus/post-me-demo/commit/5564993bf8aad01d9c90be27cf82b5e63adf0817

The big caveat is that the AudioWorklet must use the ChildHandshake, because ParentHandshake uses setTimeout which is not available in the AudioWorkletGlobalScope. This would be worthwhile highlighting in the docs.

I also added a contrived example where the Parent connects the Worker and AudioWorklet via MessageChannel so they can talk to each other. Parent then calls sum on the AudioWorklet, which delegates the call to the Worker.

I think maybe where I tripped up before was possibly making the setWorkerPort and setWorkletPort return promises and awaiting them, meaning the handshake timed out. It could also be something weird with create-react-app or webpack

grahamlyus avatar Feb 10 '21 22:02 grahamlyus

@grahamlyus Really glad to hear it worked out!

I checked out your demo, it's a pretty cool setup with the worker/worklet directly connected over the channel, and I'm quite relieved it actually worked! :) I'm curious to see what you're building with it, feel free to follow up once you're finished.

Btw, methods can return either values or promises, it shouldn't make a difference to the library.

Good find, I didn't consider that some contexts might not have setTimeout. You're welcome to open a PR mentioning it in the docs and maybe a mention to usage with worklets too.

If you don't have time, I can probably get around to adding it some time over the weekend.

alesgenova avatar Feb 10 '21 23:02 alesgenova

@alesgenova I dropped it into my project and it all seems to still be working well.

I'm experimenting with audio processing with WebAssembly. The Web Worker is used to download and decode chunks of an audio stream on demand when the Audio Worklet asks for them, as the process function has to be quick and the Audio Worklet only has access to a limited api anyway.

I can try adding a small readme section for Audio Worklet usage it that sounds good.

grahamlyus avatar Feb 11 '21 06:02 grahamlyus

Sounds good to me, I think it's a cool use case

alesgenova avatar Feb 11 '21 13:02 alesgenova