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

@hetrodo/webrocket

v1.0.4

Published

WebRocket is a framework for WebSockets that create an abstraction on top of WebSockets just like http servers are on top of TCP/IP servers.

Downloads

3

Readme

WebRocket Logo

Build status Downloads

WebRocket is a framework for WebSockets, its purpose is to create an abstraction on top of WebSockets just like http servers are on top of TCP/IP servers.

WebRocket simplify WebSockets usage by creating a rest api like interface.

Client example project

Server example project

Client

yarn add @hetrodo/webrocket
const WebRocket = require('@hetrodo/webrocket/lib/WebRocket');
const WebRocketMethod = require('@hetrodo/webrocket/lib/WebRocketMethod');
const WebSocketAdapter = require('@hetrodo/webrocket/lib/WebSocketAdapter');

//First we connect to the WebSocket server
const ws = new WebSocket('ws://127.0.0.1:8080');

//We wait for the connection to open
ws.onopen = () => {
    //With the connection open we instantiate the WebRocket class
    const clientAdapter = new WebSocketAdapter(ws);
    const webRocket = new WebRocket(clientAdapter);

    //Now we can register the client-side endpoints that we want using GET, POST, PUT, DELETE methods.
    webRocket.on(WebRocketMethod.get, 'v1/client-entity', (request, respond) => {
        respond({
            msg: 'Ok'
        });
    });

    webRocket.on(WebRocketMethod.post, 'v1/client-entity', (request, respond) => {
        respond({
            msg: 'Ok'
        });
    });

    webRocket.on(WebRocketMethod.put, 'v1/client-entity', (request, respond) => {
        respond({
            msg: 'Ok'
        });
    });

    webRocket.on(WebRocketMethod.delete, 'v1/client-entity', (request, respond) => {
        respond({
            msg: 'Ok'
        });
    });

    //And using the WebRocket's class instance we can make requests to the server-side defined endpoints
    webRocket.get('v1/server-entity').then(console.log).catch(console.error);
    webRocket.post('v1/server-entity', {}).then(console.log).catch(console.error);
    webRocket.put('v1/server-entity', {}).then(console.log).catch(console.error);
    webRocket.delete('v1/server-entity').then(console.log).catch(console.error);

    //You can use query params too
    webRocket.get('v1/server-entity?key=value').then(console.log).catch(console.error);
};

Server

yarn add express ws @hetrodo/webrocket
const express = require('express');
const { createServer } = require('http');
const { WebSocketServer } = require('ws');
const WebRocket = require('@hetrodo/webrocket');
const WebRocketMethod = require('@hetrodo/webrocket/lib/WebRocketMethod');
const WebSocketAdapter = require('@hetrodo/webrocket/lib/WebSocketAdapter');

//Create your WebSocket server
const app = express();
const server = createServer(app);
const wss = new WebSocketServer({ server });

wss.on('connection', function (ws) {
    //When a connection established we can instantiate a new WebRocket.
    const webRocket = new WebRocket(new WebSocketAdapter(ws));

    //Now we can register the server-side endpoints that we want using GET, POST, PUT, DELETE methods.
    webRocket.on(WebRocketMethod.get, 'v1/server-entity', (request, respond) => {
        respond({
            msg: 'Ok'
        });
    });

    webRocket.on(WebRocketMethod.post, 'v1/server-entity', (request, respond) => {
        respond({
            msg: 'Ok'
        });
    });

    webRocket.on(WebRocketMethod.put, 'v1/server-entity', (request, respond) => {
        respond({
            msg: 'Ok'
        });
    });

    webRocket.on(WebRocketMethod.delete, 'v1/server-entity', (request, respond) => {
        respond({
            msg: 'Ok'
        });
    });

    //And using the WebRocket's class instance we can make requests to the client-side defined endpoints
    webRocket.get('v1/client-entity').then(console.log).catch(console.error);
    webRocket.post('v1/client-entity', {}).then(console.log).catch(console.error);
    webRocket.put('v1/client-entity', {}).then(console.log).catch(console.error);
    webRocket.delete('v1/client-entity').then(console.log).catch(console.error);

    //Just like in the client side you can use query params too
    webRocket.get('v1/client-entity?key=value').then(console.log).catch(console.error);
});

server.listen(8080, function () {
    console.log('Listening on http://localhost:8080');
});

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT