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 🙏

© 2026 – Pkg Stats / Ryan Hefner

nbrc-csp

v0.0.10

Published

***C***ommunication ***Sp***ace between JavaScript modules

Readme

Communication Space between JavaScript modules

Connect && Disconnect

  1. modules have to connect to same space to communicate with others
  2. make sure to disconnect when instance or component is destroyed

A.js ( React Function Component )

import { useEffect } from 'react';
import csp from 'nbrc-csp';

function A(props) {

    conn = null;

    useEffect(() => {
        // csp.connect(connection_name = '', space_name = '_default_')
        conn = csp.connect() // connect to '_default_' space with no name
        return function () {
            // disconnect from '_default_' space
            conn.disconnect();
        }
    }, []);

    return <>Component A</>;
}

export default A;

B.js ( React Class Component )

import React from 'react';
import csp from 'nbrc-csp';

class B extends React.Component {

    conn = null;

    componentDidMount() {
        this.conn = csp.connect() // connect to '_default_' space with no name
    }

    componentWillUnmount() {
	// disconnect from '_default_' space
        this.conn.disconnect();
    }

    render() {
        return <>Component B</>;
    }
}

export default B;

Events

Trigger Events ( A.js )

import { useEffect } from 'react';
import csp from 'nbrc-csp';

function A(props) {

    conn = null;

    useEffect(() => {
        // csp.connect(connection_name = '', space_name = '_default_')
        conn = csp.connect('ACom') // connect to '_default_' space and name this connection 'ACom'
        return function () {
            // disconnect from '_default_' space
            conn.disconnect();
        }
    }, []);

    function onClick() {
        // Trigger an event in the space and pass some parameters
        conn.trigger('A-Button-Clicked', 'param1', 123);
    }

    return <>
        <button onClick={onClick}>Trigger Event</button>
    </>
}

export default A;

Listen Events ( B.js )

import React from 'react';
import csp from 'nbrc-csp';

class B extends React.Component {

    conn = null;

    componentDidMount() {
        this.conn = csp.connect()
            .listen((event, ...params) => { // listen events in the space
                console.log(event.trigger); // who fired this event (connection name: ACom)
                console.log(event.name); // event name
                switch (event.name) {
                    case 'A-Button-Clicked':
                        console.log(...params); // 'param1'  123
                        break;
                }
            })
    }

    componentWillUnmount() {
	// disconnect from '_default_' space
        this.conn.disconnect();
    }

    render() {
        return <>Component B</>;
    }
}

export default B;

Expose

Expose to space ( A.js )

expose functions or variables to space, and can be accessed by other modules

import { useEffect } from 'react';
import csp from 'nbrc-csp';

function A(props) {

    conn = null;

    useEffect(() => {
        // csp.connect(connection_name = '', space_name = '_default_')
        conn = csp.connect('ACom') // connect to '_default_' space and name this connection 'ACom'
            .expose({ funA }) // expose funcA to space
        return function () {
            // disconnect from '_default_' space
            conn.disconnect();
        }
    }, []);

    /**
     * funcA will be exposed to space
     */
    function funA() {
        console.log('Function In A...');
    }

    function onClick() {
        // Trigger an event in the space and pass some parameters
        conn.trigger('A-Button-Clicked', 'param1', 123);
    }

    return <>
        <button onClick={onClick}>Trigger Event</button>
    </>
}

export default A;

Access Exposed Items ( B.js )

import React from 'react';
import csp from 'nbrc-csp';

class B extends React.Component {

    conn = null;

    componentDidMount() {
        this.conn = csp.connect()
            .listen((event, ...params) => { // listen events in the space
                console.log(event.trigger); // who fired this event (connection name: ACom)
                console.log(event.name); // event name
                switch (event.name) {
                    case 'A-Button-Clicked':
                        console.log(...params); // 'param1'  123
                        break;
                }
            })
    }

    componentWillUnmount() {
	// disconnect from '_default_' space
        this.conn.disconnect();
    }

    callA() {
        // call function in A Component by A's connction name ACom
        this.conn.getExpose().ACom.funcA();
    }

    render() {
        return <>
            <Button onClick={callA}>Call Function in A Component</Button>
        </>;
    }
}

export default B;

Connection Name

You can connect to a space with no name or a unique name.

But if you want to expose some thing( functions or variables ) to space, you must name the connection, because other modules have to invoke the exposed functions or access exposed variables by the connection name.

Multiple Spaces

An instance can have multiple spaces, distinguished by space names. No need to manually create a space, when connecting to a space, if the space name does not exist, it will be automatically created.

Only modules in the same space can communicate with each other.

Examples:

// csp.connect(connection_name = '', space_name = '_default_')
conn = csp.connect('','A'); // connect to space 'A' with no name
conn = csp.connect('c1','A'); // connect to space 'A' with name 'c1'
conn = csp.connect('c2','B'); // connect to space 'B' with name 'c2'
conn = csp.connect('c3'); // connect to space '_default_' with name 'c3'

Multiple Instances

import csp from 'nbrc-csp';

let newCSP = csp.createInstance();