Messaging
Communicate between your extension's components.
Messaging API makes communication between different parts of your extension easy. Add a file to your background messages
directory, and Plasmo will handle all the rest.
Plasmo Messaging is a declarative, type-safe, functional, promise-based API for sending, relaying, and receiving messages between your extension components.
Handling messages
The @plasmohq/messaging
library requires the background service worker to live inside a ap/background/index.ts
folder, and all message handlers to live inside app/background/messages/*
folders.
For example, TurboStarter comes with predefined message handler for retrieving and parsing auth session. It's defined in app/background/messages/session.ts
and the file structure looks like this:
To create a message handler, create a ts module in the background/messages
directory. The file name should be the message name, and the default export should be the handler function:
It's fully type-safe!
On compilation, Plasmo will generate static types for all of your message handlers. This means that you can't accidentally send the wrong type of message.
Sending messages
Extension pages, content scripts, or tab pages can send messages to the handlers using the @plasmohq/messaging
library. Since Plasmo Framework orchestrates your handlers behind the scenes, the message names are typed and will enable autocompletion in your editor:
As it's an asynchronous operation, it's advisable to use @tanstack/react-query integration to handle the response on the client side.
We're already doing it that way when fetching auth session in the User
component:
E2E type safety is in progress on Plasmo side
As you can see, we're explicitly defining the type using
generics
together with sendToBackground
function. It's because currently Plasmo
doesn't support extracting message handler types automatically. It's in
progress, and we'll migrate to
it in the future.
Last updated on