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

Expose WebHID to Web Workers #120

Open
razvanphp opened this issue Sep 6, 2024 · 13 comments
Open

Expose WebHID to Web Workers #120

razvanphp opened this issue Sep 6, 2024 · 13 comments

Comments

@razvanphp
Copy link

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
Copy link
Contributor

beaufortfrancois commented Sep 6, 2024

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

@razvanphp
Copy link
Author

@beaufortfrancois
Copy link
Contributor

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

@beaufortfrancois
Copy link
Contributor

beaufortfrancois commented Sep 6, 2024

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Contributor

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

How did it go with your devices?

aarongable pushed a commit to chromium/chromium that referenced this issue Sep 18, 2024
This CL exposes the WebHID API to dedicated workers behind the blink
runtime feature WebHIDOnDedicatedWorkers.

Intent to prototype: https://groups.google.com/a/chromium.org/g/blink-dev/c/y__BOYfZWzI
Spec PR: WICG/webhid#121
Spec issue: WICG/webhid#120
Demo: https://webhid-worker.glitch.me/

Change-Id: If145f34faf9211a54c8b4a15fbe7e903411b8d1d
Bug: 365932453
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5841991
Reviewed-by: Arthur Sonzogni <[email protected]>
Reviewed-by: Yoav Weiss (@Shopify) <[email protected]>
Reviewed-by: Matt Reynolds <[email protected]>
Commit-Queue: Fr <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1357046}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Sep 18, 2024
This CL exposes the WebHID API to dedicated workers behind the blink
runtime feature WebHIDOnDedicatedWorkers.

Intent to prototype: https://groups.google.com/a/chromium.org/g/blink-dev/c/y__BOYfZWzI
Spec PR: WICG/webhid#121
Spec issue: WICG/webhid#120
Demo: https://webhid-worker.glitch.me/

Change-Id: If145f34faf9211a54c8b4a15fbe7e903411b8d1d
Bug: 365932453
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5841991
Reviewed-by: Arthur Sonzogni <[email protected]>
Reviewed-by: Yoav Weiss (@Shopify) <[email protected]>
Reviewed-by: Matt Reynolds <[email protected]>
Commit-Queue: Fr <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1357046}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Sep 18, 2024
This CL exposes the WebHID API to dedicated workers behind the blink
runtime feature WebHIDOnDedicatedWorkers.

Intent to prototype: https://groups.google.com/a/chromium.org/g/blink-dev/c/y__BOYfZWzI
Spec PR: WICG/webhid#121
Spec issue: WICG/webhid#120
Demo: https://webhid-worker.glitch.me/

Change-Id: If145f34faf9211a54c8b4a15fbe7e903411b8d1d
Bug: 365932453
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5841991
Reviewed-by: Arthur Sonzogni <[email protected]>
Reviewed-by: Yoav Weiss (@Shopify) <[email protected]>
Reviewed-by: Matt Reynolds <[email protected]>
Commit-Queue: Fr <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1357046}
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Sep 25, 2024
…s, a=testonly

Automatic update from web-platform-tests
WebHID: Add support for dedicated workers

This CL exposes the WebHID API to dedicated workers behind the blink
runtime feature WebHIDOnDedicatedWorkers.

Intent to prototype: https://groups.google.com/a/chromium.org/g/blink-dev/c/y__BOYfZWzI
Spec PR: WICG/webhid#121
Spec issue: WICG/webhid#120
Demo: https://webhid-worker.glitch.me/

Change-Id: If145f34faf9211a54c8b4a15fbe7e903411b8d1d
Bug: 365932453
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5841991
Reviewed-by: Arthur Sonzogni <[email protected]>
Reviewed-by: Yoav Weiss (@Shopify) <[email protected]>
Reviewed-by: Matt Reynolds <[email protected]>
Commit-Queue: Fr <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1357046}

--

wpt-commits: f3a05bb7fe21b6e6d8fbf5923d53abfc7c04fd66
wpt-pr: 48244
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this issue Sep 26, 2024
…s, a=testonly

Automatic update from web-platform-tests
WebHID: Add support for dedicated workers

This CL exposes the WebHID API to dedicated workers behind the blink
runtime feature WebHIDOnDedicatedWorkers.

Intent to prototype: https://groups.google.com/a/chromium.org/g/blink-dev/c/y__BOYfZWzI
Spec PR: WICG/webhid#121
Spec issue: WICG/webhid#120
Demo: https://webhid-worker.glitch.me/

Change-Id: If145f34faf9211a54c8b4a15fbe7e903411b8d1d
Bug: 365932453
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5841991
Reviewed-by: Arthur Sonzogni <arthursonzognichromium.org>
Reviewed-by: Yoav Weiss (Shopify) <yoavweisschromium.org>
Reviewed-by: Matt Reynolds <mattreynoldschromium.org>
Commit-Queue: Fr <beaufort.francoisgmail.com>
Cr-Commit-Position: refs/heads/main{#1357046}

--

wpt-commits: f3a05bb7fe21b6e6d8fbf5923d53abfc7c04fd66
wpt-pr: 48244

UltraBlame original commit: 091c9c912077b35fe3eb0526c59790da67a80a9a
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this issue Sep 26, 2024
…s, a=testonly

Automatic update from web-platform-tests
WebHID: Add support for dedicated workers

This CL exposes the WebHID API to dedicated workers behind the blink
runtime feature WebHIDOnDedicatedWorkers.

Intent to prototype: https://groups.google.com/a/chromium.org/g/blink-dev/c/y__BOYfZWzI
Spec PR: WICG/webhid#121
Spec issue: WICG/webhid#120
Demo: https://webhid-worker.glitch.me/

Change-Id: If145f34faf9211a54c8b4a15fbe7e903411b8d1d
Bug: 365932453
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5841991
Reviewed-by: Arthur Sonzogni <arthursonzognichromium.org>
Reviewed-by: Yoav Weiss (Shopify) <yoavweisschromium.org>
Reviewed-by: Matt Reynolds <mattreynoldschromium.org>
Commit-Queue: Fr <beaufort.francoisgmail.com>
Cr-Commit-Position: refs/heads/main{#1357046}

--

wpt-commits: f3a05bb7fe21b6e6d8fbf5923d53abfc7c04fd66
wpt-pr: 48244

UltraBlame original commit: 091c9c912077b35fe3eb0526c59790da67a80a9a
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this issue Sep 26, 2024
…s, a=testonly

Automatic update from web-platform-tests
WebHID: Add support for dedicated workers

This CL exposes the WebHID API to dedicated workers behind the blink
runtime feature WebHIDOnDedicatedWorkers.

Intent to prototype: https://groups.google.com/a/chromium.org/g/blink-dev/c/y__BOYfZWzI
Spec PR: WICG/webhid#121
Spec issue: WICG/webhid#120
Demo: https://webhid-worker.glitch.me/

Change-Id: If145f34faf9211a54c8b4a15fbe7e903411b8d1d
Bug: 365932453
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5841991
Reviewed-by: Arthur Sonzogni <arthursonzognichromium.org>
Reviewed-by: Yoav Weiss (Shopify) <yoavweisschromium.org>
Reviewed-by: Matt Reynolds <mattreynoldschromium.org>
Commit-Queue: Fr <beaufort.francoisgmail.com>
Cr-Commit-Position: refs/heads/main{#1357046}

--

wpt-commits: f3a05bb7fe21b6e6d8fbf5923d53abfc7c04fd66
wpt-pr: 48244

UltraBlame original commit: 091c9c912077b35fe3eb0526c59790da67a80a9a
@beaufortfrancois
Copy link
Contributor

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

@razvanphp
Copy link
Author

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
Copy link
Contributor

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
Copy link

@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
Copy link
Contributor

Thank you @chrisdavidmills! It is just for dedicated workers and service workers for now. See #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

jamienicol pushed a commit to jamienicol/gecko that referenced this issue Sep 26, 2024
…s, a=testonly

Automatic update from web-platform-tests
WebHID: Add support for dedicated workers

This CL exposes the WebHID API to dedicated workers behind the blink
runtime feature WebHIDOnDedicatedWorkers.

Intent to prototype: https://groups.google.com/a/chromium.org/g/blink-dev/c/y__BOYfZWzI
Spec PR: WICG/webhid#121
Spec issue: WICG/webhid#120
Demo: https://webhid-worker.glitch.me/

Change-Id: If145f34faf9211a54c8b4a15fbe7e903411b8d1d
Bug: 365932453
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5841991
Reviewed-by: Arthur Sonzogni <[email protected]>
Reviewed-by: Yoav Weiss (@Shopify) <[email protected]>
Reviewed-by: Matt Reynolds <[email protected]>
Commit-Queue: Fr <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1357046}

--

wpt-commits: f3a05bb7fe21b6e6d8fbf5923d53abfc7c04fd66
wpt-pr: 48244
@chrisdavidmills
Copy link

@beaufortfrancois mdn/content#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.

i3roly pushed a commit to i3roly/firefox-dynasty that referenced this issue Sep 27, 2024
…s, a=testonly

Automatic update from web-platform-tests
WebHID: Add support for dedicated workers

This CL exposes the WebHID API to dedicated workers behind the blink
runtime feature WebHIDOnDedicatedWorkers.

Intent to prototype: https://groups.google.com/a/chromium.org/g/blink-dev/c/y__BOYfZWzI
Spec PR: WICG/webhid#121
Spec issue: WICG/webhid#120
Demo: https://webhid-worker.glitch.me/

Change-Id: If145f34faf9211a54c8b4a15fbe7e903411b8d1d
Bug: 365932453
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5841991
Reviewed-by: Arthur Sonzogni <[email protected]>
Reviewed-by: Yoav Weiss (@Shopify) <[email protected]>
Reviewed-by: Matt Reynolds <[email protected]>
Commit-Queue: Fr <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1357046}

--

wpt-commits: f3a05bb7fe21b6e6d8fbf5923d53abfc7c04fd66
wpt-pr: 48244
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

3 participants