msgr
Nifty worker/client postMessage utility
Made with ❤ at @outlandish
Makes communication between a client and service worker super easy...
- Send messages from
client -> worker
andworker -> client
with one call tochannel.send()
- Simple API: send anonymous data-only messages, typed-only messages, or typed messages with data
- Register handlers for typed messages and anonymous messages
- Easily respond to any message by calling
respond()
in the handler - Receive a response for a message using the familiar Promise
then()
method
Table of Contents
Import
// ES6 // CommonJSvar msgr = // RequireJS
<!-- Script, available as `window.msgr` -->
Initialise & Example
Client: msgr.client()
Pass in reference to the worker and a collection of message handlers:
const recipient = navigatorserviceWorkercontroller const channel = msgr // Send something "unknown" to the worker.// Notice it does not have a tag.channel // Send a "known" message to the workerchannel
Worker: msgr.worker()
On the worker you just pass in your message handlers:
const channel = msgr channel // Send something "known" to the client using a tag.channel { }
msgr API
msgr.client(serviceWorker, handlers) : Channel
Initialise a msgr
client.
- serviceWorker {ServiceWorkerRegistration} Worker that will receive messages sent via channel
- handlers {Object} An object of message type/handler mappings
Returns a Channel. See the Channel API Docs for more details.
Example:
msgr
msgr.worker(handlers) : Channel
Initialise a msgr
worker.
- handlers {Object} An object of message type/handler mappings
Returns a Channel. See the Channel API Docs for more details.
Example:
msgr
Channel API
channel.ready(handler)
Register a handler to be called when the channel is opened between client and worker.
- handler {Function} The ready handler
Although you can register ready handlers, you can send messages before the channel is open using
channel.send()
and these will be queued and sent as soon as the channel is ready.
Example:
channel
channel.send([type,] data) : Promise
Send a message through the channel to the worker/client.
- [type] {String} (optional) The message type
- data {Any} The message data (it will be JSON.stringify'd)
Returns a Message. See the Message API Docs for more details.
If called before the channel is ready the message will be queued and sent as soon as the channel is open.
Example:
// Typed message, will invoke registered type handlerschannel // Typed message with data, will invoke registered type handlerschannel // Anonymous, will invoke `receive` handlerschannel
channel.receive(handler)
Handle an "unknown" message that is not tagged.
- handler {Function} The message handler
The handler receives two arguments: the data
of the message and a respond
function.
Example:
channel
Message API
message.then(handler)
Register a handler to receive the response to a message.
- handler {Function} Response handler
Example:
// In client message handlers // In workerchannel
respond([data])
Send a response to a received message.
This function is passed as the second argument to both "known" and "unknown" message handlers.
- [data] {Any} (optional) The data to respond to the message with
Contributing
All pull requests and issues welcome!
If you're not sure how, check out Kent C. Dodds' great video tutorials on egghead.io!
Author & License
msgr
was created by Sam Gluck and is released under the MIT license.