WICG / webhid

Web API for accessing Human Interface Devices (HID)
Other
143 stars 35 forks source link

Expose WebHID to Web Workers #120

Open razvanphp opened 2 months ago

razvanphp commented 2 months ago

We are developing an in-browser racing game that relies heavily on gamepads and particularly complex racing wheels and pedals.

Given the differences in hardware, lack of support for Vibration and Haptic Feedback and pooling rather that event-based access, we decided to implement our control layer using WebHID instead of Gamepad API.

To be able to provide the lowest possible latency, we need to make sure the inputs are sent over to the backend ASAP (websockets in our case), without blocking the main thread, hence the need to access this API inside web workers is needed.

We see the same was done for service-workers (in extensions) in #97, so I assume this change is very similar and easy to add, the requestDevice should still reside in the main thread only.

Hope this is sufficient details for our use-case, otherwise we are happy for an open discussion, CCing @nondebug and @chengweih001 in.

Thank you in advance!

beaufortfrancois commented 2 months ago

My naive understanding is that it would be similar to WebUSB on Workers

razvanphp commented 2 months ago

Indeed, and that proposal was already accepted: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers#web_apis_available_in_workers

beaufortfrancois commented 2 months ago

@nondebug @chengweih001 I'm happy to update the spec and implement this in Chromium following what was done for WebUSB if that helps

beaufortfrancois commented 2 months ago

FYI, I've started a Chromium CL that adds support for Dedicated Workers to WebHID at https://chromium-review.googlesource.com/c/chromium/src/+/5841991 and it works great (so far) on my machine !

I've created https://webhid-worker.glitch.me/ to play with it. All you need is this Chromium patch and run $ ./out/Debug/Chromium.app/Contents/MacOS/Chromium --enable-blink-features=WebHIDOnDedicatedWorkers at the moment.

image

razvanphp commented 2 months ago

Looks great! I will test it with our devices too.

BTW: why only dedicated workers and not shared ones as well?

Thank you for the quick reaction!

beaufortfrancois commented 2 months ago

BTW: why only dedicated workers and not shared ones as well?

I believe it could be implemented in shared workers as well but I didn't look into it yet.

beaufortfrancois commented 2 months ago

Looks great! I will test it with our devices too.

How did it go with your devices?

beaufortfrancois commented 2 months ago

🎉 Support for dedicated workers in WebHID is now enabled by default in Chrome 131.0.6740.0.

razvanphp commented 2 months ago

Screenshot 2024-09-26 095954

I can confirm it is working beautifully! Thank you for the quick reaction!

PS: we should update the list of APIs available in workers here: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers#web_apis_available_in_workers

beaufortfrancois commented 2 months ago

I can confirm it is working beautifully! Thank you for the quick reaction!

I'm happy about it as well! Please let us know when your web app is ready so that others can try it ;)

PS: we should update the list of APIs available in workers here: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Functions_and_classes_available_to_workers#web_apis_available_in_workers

@chrisdavidmills may be able to help us!

chrisdavidmills commented 2 months ago

@beaufortfrancois sure thing, I can do that.

It might also make sense to add browser compat data entries to show when the WebHID interfaces were made available to web workers, in the same style as this: https://github.com/mdn/browser-compat-data/blob/63cd0efe688ecd6a7fe86bf65b09e82762a04b23/api/_globals/indexedDB.json#L50

Can you confirm which interfaces/features of https://developer.mozilla.org/en-US/docs/Web/API/WebHID_API are specifically exposed to workers, so I know where to put these entries?

And it is just dedicated workers, for now, correct?

beaufortfrancois commented 2 months ago

Thank you @chrisdavidmills! It is just for dedicated workers and service workers for now. See https://github.com/WICG/webhid/pull/121

[Exposed=(DedicatedWorker,ServiceWorker), SecureContext]
partial interface WorkerNavigator {
    [SameObject] readonly attribute HID hid;
};

[Exposed=(DedicatedWorker,ServiceWorker,Window), SecureContext]
interface HID
interface HIDDevice
interface HIDConnectionEvent
interface HIDInputReportEvent
chrisdavidmills commented 2 months ago

@beaufortfrancois https://github.com/mdn/content/pull/36060.

Can you provide a tech review? Shouldn't take long, as the vast majority of the changes are just adding "supported in workers" banners to existing pages.