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 🙏

© 2025 – Pkg Stats / Ryan Hefner

broadcast-channel-storage

v0.1.2

Published

localStorage-like API to share temporary state across tabs using Broadcast Channel.

Downloads

4

Readme

broadcast-channel-storage

localStorage-like API to share temporary state across tabs using Broadcast Channel.

Installation

npm install broadcast-channel-storage

Usage

Importing the Library

import { BroadcastChannelStorage } from 'broadcast-channel-storage';

Creating an Instance

const storage = new BroadcastChannelStorage();

You can also provide optional values to the constructor:

const storage = new BroadcastChannelStorage({
  channelName: 'my-channel', // BroadcastChannel name. Default is '__broadcast-channel-storage'
  responseTimeoutMs: 200, // Timeout while waiting for response from sync/ready. Default is 200ms
});

Setting an Item

storage.setItem('key', 'value');

Getting an Item

const value = storage.getItem('key');

Removing an Item

storage.removeItem('key');

Clearing All Items

storage.clear();

Waiting for Ready State

Although the getItem method can be run at any time. You may wish to wait until the initial sync has completed before getting the value:

await storage.ready();
const value = storage.getItem('key');
console.log(value);

Forcing a Sync

You can force a sync using the sync() method:

await storage.sync();

Listening for Changes

Changes made to current instance

storage.addEventListener('change', (event) => {
  console.log(
    `Key ${event.key} changed from ${event.oldValue} to ${event.newValue}`,
  );
});

change events are fired for the current instance (that the listener was added to) when a value is changed on it via setItem, removeItem or clear.

Changes made to other instances, after syncing to current instance

storage.addEventListener('storage', (event) => {
  console.log(
    `Key ${event.key} changed from ${event.oldValue} to ${event.newValue}`,
  );
});

Note: Similar to the original localStorage storage event, changes made in one BroadcastChannelStorage instance will emit storage events for other instances, but not for the instance where the change was made.

Properties

  • length: Gets the number of values.
  • values: Returns all the values.
  • isReady: A flag that is true when the initial sync has happened.
  • isClosed: A flag that is true when the channel is closed.
  • isLastInstance: A flag that is true when it is known that this is the final BroadcastChannelStorage instance.

To force updating the isLastInstance property:

await storage.sync();
console.log(storage.isLastInstance);

Events

The library extends EventTarget and emits the following events:

  • change: Emitted on current instance when a storage item is changed.
  • storage: Emitted on other instances when a storage item is changed.
  • ready: Emitted when the initial sync has completed.
  • closed: Emitted when the current storage instance is closed.
  • last-instance: Emitted when the isLastInstance flag changes.

The change and storage events are similar, a subset of a standard storage event:

{
  key: string | null;
  oldValue: string | null;
  newValue: string | null;
}

The last-instance event:

{
  isLastInstance: boolean;
}

API

setItem(key: string, value: string): void

Sets the value for the specified key.

getItem(key: string): string | null

Gets the value for the specified key.

removeItem(key: string): void

Removes the specified key.

clear(): void

Clears all keys.

addEventListener(event: string, callback: Function): void

Registers an event listener for the specified event.

ready(): Promise<void>

Waits until the initial sync has happened.

sync(): Promise<void>

Forces a sync with other instances.