Skip to Content

nebula_sw_message

This is a custom DOM event to conveniently listen for Service Worker messages.

JavaScript February 19, 2018

Usage

This is a variable or event handle.

Examples

Listen for all messages

JavaScript
jQuery(document).on('nebula_sw_message', function(e, message){
    console.log('Message received via Nebula event:', message);
});

Listen for a message with an object key of "demo"

JavaScript
jQuery(document).on('nebula_sw_message', function(e, message){
    if ( message.demo ){
        console.log('Message received via Nebula event:', message.demo);
    }
});

Demo


Waiting for JavaScript to load...

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

Additional Notes

This is useful for syncing data/information between tabs without needing localstorage. Like closing a notification in one tab closes it in all tabs, or incrementing a counter on all tabs (like an ecommerce cart).

Note: the message is not sent to the same tab/window (client) that triggered it.

This event will trigger anytime the service worker receives a message, so you may need to check to make sure you are acting on the correct one.

To post a message to the service worker, use navigator.serviceWorker.controller.postMessage(data), or the more convenient Nebula alias: nebulaPostMessage

Source File

Located in /assets/js/nebula.js.