New Chrome messaging library: extension-bus

332 views
Skip to first unread message

Likely Logic

unread,
Jan 16, 2024, 7:57:25 AM1/16/24
to Chromium Extensions
Hi all,

I just posted a messaging library for Chrome and Firefox, MV2 and MV3 extensions:
It has a bunch of useful features, mainly:
  • cross-process messaging
  • named buses to easily target individual processes
  • named and nested handlers to support more complex use cases
  • transparent handling of sync and async calls
  • transparent handling of process and runtime errors
  • a consistent interface for processes and tabs
Effectively, you set up endpoints for each process, and every other bus can call and target those endpoints via path.

I've been running a version of it for the last few years, but have recently rewritten it and decided to open source it. I've included in both the code and the docs my accumulated knowledge on all things messaging, so feel free to use, comment or critique.

Best wishes,
Dave

Oliver Dunk

unread,
Jan 16, 2024, 8:03:17 AM1/16/24
to Likely Logic, Chromium Extensions
Hey Dave,

This is great - thank you so much for sharing! Would love to hear your experience building it and what challenges you ran into.

I'll have a play soon but appreciate the really comprehensive README in the repository.

Is there any typing for message request and response payloads yet? If not, that would be a cool addition.
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/0acf3925-70fe-4e81-87da-786d18ad6c61n%40chromium.org.

Likely Logic

unread,
Jan 16, 2024, 8:10:57 AM1/16/24
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Likely Logic
Hey Oliver!

You mean making the function call generic, or some kind of build process to generate types for the nested handlers?

I considered typing the call function, but as it guaranteed to succeed (even if returning null) is there any advantage over just const result: SomeType = await bus.call('path/to/handler') ?

I'm open to suggestions / corrections of assumptions!

Likely Logic

unread,
Jan 16, 2024, 8:43:44 AM1/16/24
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Likely Logic
Perhaps something like this?

https://github.com/davestewart/extension-bus/pull/3

On Tuesday 16 January 2024 at 13:03:17 UTC Oliver Dunk wrote:

Oliver Dunk

unread,
Jan 16, 2024, 11:20:31 AM1/16/24
to Likely Logic, Chromium Extensions
Exactly like your PR!

I just replied there with some thoughts but generally that's what I was thinking.
Oliver Dunk | DevRel, Chrome Extensions | https://developer.chrome.com/ | London, GB

Likely Logic

unread,
Jan 17, 2024, 4:52:46 AM1/17/24
to Chromium Extensions, Oliver Dunk, Chromium Extensions, Likely Logic
Grand :)

I've added some Chat GPT research about correctly typing the paths too, so potentially all handler paths, their payloads and return types could be compiled to aid with autocomplete.

Andrzej Karaś

unread,
Jan 19, 2024, 9:56:16 AM1/19/24
to Chromium Extensions, Likely Logic, Oliver Dunk, Chromium Extensions
Hi Dave,

Your event bus looks very well crafted and documented code.

A year ago I created something way similar. It's also implemented differently compared to your solution. But it is not so feature-rich, yet. I did not have time to work on this.


Best wishes,
Andrzej

Likely Logic

unread,
Jan 21, 2024, 5:25:25 AM1/21/24
to Chromium Extensions, Andrzej Karaś, Likely Logic, Oliver Dunk, Chromium Extensions
Hey Andrzej!

Always good to review other approaches!

Are you leveraging storage events there to simulate messaging?

Andrzej Karaś

unread,
Jan 31, 2024, 6:51:18 AM1/31/24
to Chromium Extensions, Likely Logic, Andrzej Karaś, Oliver Dunk, Chromium Extensions
> Are you leveraging storage events there to simulate messaging?

Exactly. This allows me to react to events without implementing custom code. When webextension-polyfill will support session storage I will add also quasi in-memory event bus.
Reply all
Reply to author
Forward
0 new messages