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

safety-socket

v1.0.2

Published

Easy to set up, super safe websocket connection library

Downloads

3

Readme

safety-socket Library

The safety-socket library offers a simple solution for managing WebSocket communications, ensuring encrypted and verified interactions between peers.

Installation

npm install safety-socket

Usage

Importing the Library

import { SafetySocket } from "safety-socket";

SafetySocket

The SafetySocket class exposes an attribute called manager, which is will be how you'll interact with WebSocket communicaiton.

Here's how to initialize it:

const safetySocket = new SafetySocket(clientApiKey);
await safetySocket.load();
const manager = safetySocket.manager;

Methods

  • connect(): Connects to the WebSocket server.

    await manager.connect();
  • kill(): Closes the WebSocket connection and resets the event listeners.

    await manager.kill();
  • on(event, handler): Adds an event listener for events.

    manager.on("communication", (event) => {
      console.log("message:", event);
    });

The main event types are communication, close, disconnect, and error.

Put handlers on these (or at least communication).

  • send(peerId, dataString): Encrypts and sends data to a peer.

    await manager.send("peer123", "Hello, peer!");
  • getSecretStatus(peerId): Checks if a secret exists for a given peer.

    let secretStatus = await manager.getSecretStatus("peer123");
    console.log(secretStatus ? "Secret is Set" : "Secret is NOT Set");
  • makeSecret(peerId): Creates a new secret for a given peer.

    manager.makeSecret("peer123").then(secret => {
      console.log("New secret:", secret);
    });
  • setSecret(peerSecret): Sets a secret for a peer.

    manager.setSecret("mySecret123").then(() => {
      console.log("Secret set.");
    });

Properties

  • id: Gets the ID of the current socket instance.

    console.log("Socket ID:", manager.id);
  • manualSecret: Sets a secret manually, and uses this for encryption/decryption between clients. Not recommended unless you know what you're doing.

    manager.manualSecret = "myManualSecret";
    console.log("Manual Secret:", manager.manualSecret);

Example

Here's a simple example that will run in the browser's console, and shows how two browser can communicate:



        import {SafetySocket} from "./index.mjs";

        // Simple ASYNC Wait Function
        async function wait (time) {
            return new Promise((s,j) => {
                setTimeout(() => {
                    s(true);
                },time)
            })
        }


        async function main() {

            // Tell everyone that the program is running
            console.log("I AM RUNNING!");

            // Instantiate client 1, load it
            let client1 = new SafetySocket();
            await client1.load();

            // Connect it.
            let manager1 = client1.manager;
            await manager1.connect();

            // SET UP WHAT TO DO WHEN WE GET A MESSAGE
            manager1.on("communication", (evt) => {console.log(`1 > MY PEER SAYS: ${evt.detail.communication}`);});

            // CLIENT 1 SHARES THEIR ID WITH A QR CODE
            // ...
            // CLIENT 2 SCANS IT

            // Instantiate client 2, load it.
            let client2 = new SafetySocket();
            await client2.load();

            // Connect it.
            let manager2 = client2.manager;
            await manager2.connect();
            
            // SET UP WHAT TO DO WHEN WE GET A MESSAGE
            manager2.on("communication", (evt) => {console.log(`2 > MY PEER SAYS: ${evt.detail.communication}`);});
            
            
            // CLIENT 2 WILL CHECK TO SEE IF IT KNOWS CLIENT 1
            let sharedSecret;

            if(! await manager2.getSecretStatus(manager1.id)){

                console.log("SETTING SECRET!!!---------------");

                // Since it does not, lets create a shared secret
                sharedSecret = await manager2.makeSecret(manager1.id);

                // Email Shared Secret to Client 1
                // ...code...

                // Client 1 sets secret. Once done, we no longer need
                // to share secrets since we'll store them in our
                // browser databases, and verify via ECDSA signatures

                await manager1.setSecret(sharedSecret);
            } else {
              console.log("...I know this client");
            }


            // Have a conversation between 1 and 2.
            manager1.send(manager2.id, "HELLO!");
            await wait(300);
            manager2.send(manager1.id, "HOLA!");
            await wait(300);
            manager1.send(manager2.id, "So...Do you like hot dogs?");
            await wait(300);
            manager2.send(manager1.id, "Ya...I DO!!!");
            await wait(300);
            manager1.send(manager2.id, "NEAT!");
            await wait(300);
            manager2.send(manager1.id, "Do you like them plain, or these new-fangled ones?");
            await wait(300);
            manager1.send(manager2.id, "There's a place here called Dog Haus...has one called the Thai-Fighter...TO DIE FOR!");
            await wait(300);
            manager2.send(manager1.id, "Give me a ballpark frank, spicy mustard, and a cold budweiser");
            await wait(1300);
            manager1.send(manager2.id, "...SO, do you like data?");
            await wait(300);
            manager2.send(manager1.id, "I love it.");
        }


        main();

License

This library is licensed under the MIT License. See the LICENSE file for more details.