Dashboard beta

Talk to your app's worker

Post messages from your app's views to one of its web workers with useWorker, and listen for what the worker sends back.

A web worker keeps running while the Dashboard is open, even when nobody is looking at your app. useWorker from @sanity/sdk-react/dashboard connects one of your app's views to one of its workers: post a message and await the worker's reply, or subscribe to the messages the worker sends out.

Beta API

Connect to a worker

The argument is the worker's name from defineWebWorker. The type parameter describes the three message shapes: payload is what the view posts, reply is what the worker returns, and message is what the worker emits on its own.

Post and await a reply

feed.post(payload, {signal, timeout}) returns a Promise that resolves with the worker's reply. If the worker never replies, the Promise rejects with a MessageBusError:

  • NO_RESPONDER: the worker isn't running.
  • TIMEOUT: the worker didn't reply in time.
  • ABORTED: the signal was aborted.
  • REFUSED: the worker called message.reject(reason).
  • HANDLER_THREW: the worker's handler threw.

Listen for the worker's messages

feed.subscribe(handler) returns an unsubscribe function, so you can return it straight from an effect.

Subscribe first, then post

Messages aren't stored. A view only receives what the worker sends while the view is listening, so subscribe first and then post to get a snapshot of the worker's current state.

The worker sends a welcome message when it starts. Listening views post again when they receive it, so a view that connected before the worker was ready fills in as soon as it is.

The connection doesn't depend on the view being visible. A panel or tile keeps posting and receiving while it is mounted, whether or not it is on screen.

Next steps

Was this page helpful?