Skip to Content

postMessage()

A convenient function for posting a message to the Service Worker.

JavaScript July 2, 2019

Usage

JavaScript
nebula.postMessage(data)

Parameters

data
(Required) (Mixed) A string, object, or valid data to pass as a message to the service worker
Default: None

Request or provide clarification »

Examples

Send a simple string as a message

JavaScript
nebula.postMessage('This is a test!');

Send an object as a message

JavaScript
nebula.postMessage({
    demo: jQuery('#message').val()
});

Demo


Waiting for JavaScript to load...

Open this page in additional tab(s) to send a message between them:

Additional Notes

This function is essentially an alias for navigator.serviceWorker.controller.postMessage(data), but it also checks if Service Worker is supported and if there is currently a controller, so it is safe to use anywhere.

Remember: Using shift+reload bypasses the service worker so the window will not be controlled! This prevents messages from being posted!

Source File

Located in /assets/js/nebula.js on line 380.

Note: This function contains 1 to-do comment.

JavaScript
nebula.postMessage = function(data){
    if ( navigator.serviceWorker && navigator.serviceWorker.controller ){ //@todo "Nebula" 0: Replace with optional chaining
        navigator.serviceWorker.controller.postMessage(data);
    }
};

Override

To override or disable this JavaScript function, simply redeclare it with the exact same function name.

JavaScript
nebula.postMessage = function(data){
    //Write your own code here, leave it blank, or return false.
}