npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sw-client-channel

v1.0.3

Published

A tool for communication between service worker and browser pages

Downloads

8

Readme

sw-client-channel

A tool for communication between service worker and browser pages

Install

npm i -S sw-client-channel

Features

  • Clients management
  • Support transferable
  • Broadcast to all clients and emit to certain client
  • Request/Respond of both sides

Usage

import ClientChannel from 'sw-client-channel/client';

navigator.serviceWorker
  .register('/sw.js')
  .then(navigator.serviceWorker.ready)
  .then(registration => {
    const channel = new ClientChannel(registration.active);

    channel.on('hi', () => {
      console.log('sw says hi');
      const arrayBuffer = new ArrayBuffer();
      channel.request('transferable', [arrayBuffer], [arrayBuffer])
        .then(buffer => {
          console.log(arrayBuffer === buffer);
          channel.emit('bye');
        });
    });

    channel.on('bye', () => {
      console.log(`sw says bye`);
    });

    channel.emit('hi');
  });
import SWChannel from 'lib/sw-client-channel/sw';

SWChannel.on('hi', clientId => {
  console.log(`client ${clientId} says hi`);
  SWChannel.emit(clientId, 'hi');
});

SWChannel.on('bye', clientId => {
  console.log(`client ${clientId} says bye`);
  channel.emit('bye');
});

SWChannel.respond('transferable', async arrayBuffer => {
  // await do sth...
  const result = arrayBuffer;
  const transferList = [arrayBuffer];
  return [result, transferList];
}, true);

should print

client <clientId> says hi
sw says hi
true
client <clientId> says bye
sw says bye

API in pages

constructor(options)

  • options.worker - The used service worker

on(name, handler)

Register an event listener to sw's

  • channel.broadcast(name, args)
  • channel.emit(clientId, name, args, transferList)

handler is executed as handler(...args)

Example
channel.on('hi', (...args) => {
  console.log(...args);
});

off(name, handler)

Unregister and event listener

Example
channel.on('hi', function handler () {
  channel.off('hi', handler);
});

emit(name, args[, transferList])

Send an request with args and optionally transferList. It is handled by sw's

  • channel.on(name, handler)
Example
channel.emit('check update');

channel.emit('give me an array buffer', [arrayBuffer], [arrayBuffer]);

request(name, args[, transferList]): Promise

Send an request with args and optionally transferList and return a promise. It is responeded by sw's

  • channel.respond(name, handler, transferable = false)
Example
channel.request('cache names', ['assets'])
  .then(console.log, console.error);

respond(name, handler, transferable = false)

Respond to a request from sw and return result. The handler can return a promise. If transferable is true, you should return [result, transferList] instead of result

Example
// transferable = false
channel.respond('what time is it', () => {
  console.log('sw asked what time is it');
  return Date.now();
});

// transferable = true
channel.respond('give me back the array buffer I sent', async arrayBuffer => {
  // await do sth...
  return [arrayBuffer, [arrayBuffer]];
}, true);

destroy()

Destroy this channel

API in service worker

on(name, handler)

Register an event listener to client's

  • channel.emit(name, args)

handler is executed as handler(clientId, ...args)

Example
channel.on('hi', (clientId, ...args) => {
  console.log(clientId, ...args);
});

off(name, handler)

Unregister and event listener

Example
channel.on('hi', function handler () {
  channel.off('hi', handler);
});

emit(clientId, name, args[, transferList])

Send an request to certain client with args and optionally transferList. It is handled by client's

  • channel.on(name, handler)
Example
channel.emit(clientId, 'reload page', [/*force = */true]);

channel.emit(clientId, 'give me an array buffer', [arrayBuffer], [arrayBuffer]);

broadcast(name, args)

Send an request to all clients with args. It is handled by client's

  • channel.on(name, handler)

**NOTE: ** it doesn't accept transferList

Example
channel.emit(clientId, 'reload page', [/*force = */true]);

channel.emit(clientId, 'give me an array buffer', [arrayBuffer], [arrayBuffer]);

request(clientId, name, args[, transferList]): Promise

Send an request to certain client with args and optionally transferList and return a promise. It is responeded by clients's

  • channel.respond(name, handler, transferable = false)
Example
channel.request(clientId, 'initialization time')
  .then(console.log, console.error);

respond(name, handler, transferable = false)

Respond to a request from client and return result. The handler can return a promise. If transferable is true, you should return [result, transferList] instead of result

handler is executed as handler(clientId, ...args)

Example
// transferable = false
channel.respond('what time is it', clientId => {
  console.log(`client ${clientId} asked what time is it`);
  return Date.now();
});

// transferable = true
channel.respond('give me back the array buffer I sent', async (clientId, arrayBuffer) => {
  // await do sth...
  return [arrayBuffer, [arrayBuffer]];
}, true);

destroy()

Destroy this channel