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

typesockets

v1.0.6

Published

An end-to-end typesafe websockets wrapper/addon through the creation of client and server endpoint functions.

Downloads

2

Readme

TypeSockets

An end-to-end typesafe websockets wrapper/addon through the creation of client and server endpoint functions.

Installing

Install this module with yarn add typesockets or npm i typesockets.

Importing

Node

const { createServer, createServerEndpoints, createClientEndpoints } = require("typesockets");

ES6

import { createServer, createServerEndpoints, createClientEndpoints } from "typesockets";

The following documentation will follow the ES6 syntax.

Creating the Server

Create the server by using the very thin wrapper function createServer. It uses the same options as the node ws module, and returns a WebsocketServer as it is just a wrapper. https://github.com/websockets/ws/blob/HEAD/doc/ws.md#class-websocketserver

server.ts

const server = createServer({ port: 8081 });

Creating the Client

Create the client by using the W3C standard for websockets: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/WebSocket

client.ts

const socket = new Websocket("ws://server-ws.url");

Server Endpoints

Server endpoints are functions that run on the server and are called from the client. To create these endpoints, run the createServerEndpoints function with the websocket server and a dictionary of functions as parameters. These endpoints can then be exported to be used client side. Then hydrate the functions with their actual functionality and attach it to the server.

serverEndpoints.ts

Creating the endpoints using empty functions with needed parameters

import { createServerEndpoints } from "typesockets";

export const serverEndpoints = createServerEndpoints( {
    log: (msg: string) => { }
});

server.ts

Hydrating and attaching the endpoints to the websocket server

import { serverEndpoints } from "../path/to/serverEndpoints.ts";

// Hydrating the functions
const hydrated = serverEndpoints.hydrate({
    log: (ws: Websocket, msg: string) => {
        console.log(string);
    }
});

// Create server
const server = createServer({ port: 8081 });

// Attach the newly hydrated functions to run on server
hydrated.attachServer(server);

client.ts

Calling the server endpoints from the client

import { serverEndpoints } from "../path/to/serverEndpoints.ts";

// Use the converted functions from the server endpoints
const extractedEndpoints = serverEndpoints.extracted;

// Create client
const socket = new Websocket("ws://server-ws.url");

// Sends websocket request to server to run command 'log' with parameter "Hello World" as msg
extractedEndpoints.log(socket, "Hello World!");

Client Endpoints

Client endpoints are similar to server endpoints, but instead call client functions from the server and do not require hydration, but still need to be attached.

clientEndpoints.ts

Creating the client functions does not require a hydration step, just directly create the functions that need to be run

// Create the functions directly
export const clientEndpoints = createClientEndpoints({
    log: (msg: string) => {
        console.log(msg);
    }
});

client.ts

Attaching the client endpoints to the websocket client

import { clientEndpoints } from "../path/to/clientEndpoints.ts";

// Create client
const socket = new Websocket("ws://server-ws.url");

// Attach client to endpoints
clientEndpoints.attachClient(socket);

server.ts

Calling the client endpoints from the server

import { clientEndpoints } from "../path/to/clientEndpoints.ts";

// Create server
const server = createServer({ port: 8081 });

// Use the converted functions from the client endpoints
const extractedEndpoints = clientEndpoints.extracted;

// If we wanted to broadcast the log function to all clients
server.clients.forEach((client) => {
    // Sends websocket request to run log on every connected client
    extractedEndpoints.log(client, "Hello World");
});

Example

TODO