When none of the built-in transports matches your channel, wrap it in a plain object with emit and receive. Anything that can move a message between two contexts (a BroadcastChannel, a text socket, a native bridge) becomes an osra transport.
A custom transport is a plain object with emit and/or receive, plus an optional isJson flag. Each of emit and receive may be a platform transport (a Worker, a WebSocket, a window, …) or a function:
An emit function is called with the ready-to-send envelope and the collected transfer list. Serialization is yours; osra does not stringify for function emitters.
A receive function is called once with osra’s listener; invoke it with parsed envelope objects. Key and remoteName filtering are applied for you. Optionally return an unsubscribe function; it runs when unregisterSignal aborts.
A transport that can’t both emit and receive rejects expose() immediately: a bare { emit } or { receive } alone is a configuration error.
Custom transports must be plain objects: prototype exactly Object.prototype, or a null prototype. This is deliberate: prototype-based objects like Node EventEmitters have inherited emit members and are intentionally not detected as custom transports, so passing one never silently misclassifies.
isJson: true forces JSON-safe boxing: base64 buffers, synthetic ports, no transfer. Set it whenever the channel can’t carry transferables.
Without it, JSON mode is auto-detected from the embedded platform transports: { emit: webSocket } is JSON-only even though the wrapper isn’t. See JSON vs clone for what degrades in JSON mode.
The second argument to osra’s receive listener carries whatever the channel knows: { port?, sender?, receiveTransport?, source?, origin? } (origin and source from window events, sender and port from WebExtension messaging, and receiveTransport).
The postMessage() method of the BroadcastChannel interface sends a message, which can be of any kind of Object, to each listener in any browsing context with the same origin.
The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.
The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.addEventListener() from the target.
The MessagePort interface of the Channel Messaging API represents one of the two ports of a MessageChannel, allowing messages to be sent from one port and listening out for them arriving at the other.
The addEventListener() method of the EventTarget interface sets up a function that will be called whenever the specified event is delivered to the target.
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON.
JSON.parse(text: string, reviver?: (this:any, key:string, value:any) => any): any
Converts a JavaScript Object Notation (JSON) string into an object.
@param ― text A valid JSON string.
@param ― reviver A function that transforms the results. This function is called for each member of the object.
If a member contains nested objects, the nested objects are transformed before the parent object is.
@throws ― {SyntaxError} If text is not valid JSON.
parse(
event: MessageEvent<any>
event.
MessageEvent<any>.data: any
The data read-only property of the The data sent by the message emitter; this can be any data type, depending on what originated this event.
emit and receive don’t have to be functions; you can compose two platform halves. The canonical case is a page talking to its service worker; a ServiceWorker can only emit and a ServiceWorkerContainer can only receive:
const
constregistration:ServiceWorkerRegistration
registration=await
var navigator:Navigator
The Window.navigator read-only property returns a reference to the Navigator object, which has methods and properties about the application running the script.
The serviceWorker read-only property of the Navigator interface returns the ServiceWorkerContainer object for the associated document, which provides access to registration, removal, upgrade, and communication with the ServiceWorker.
Available only in secure contexts.
The Window.navigator read-only property returns a reference to the Navigator object, which has methods and properties about the application running the script.
The serviceWorker read-only property of the Navigator interface returns the ServiceWorkerContainer object for the associated document, which provides access to registration, removal, upgrade, and communication with the ServiceWorker.
Available only in secure contexts.
To forward osra envelopes between two transports without terminating a connection of your own, use relay(). The primitives underneath every transport (sendOsraMessage and registerOsraMessageListener) are covered in low-level messaging.