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

ws-schema

v0.0.12

Published

provide event/payload structure with typescript & run-time safety to ws message events.

Downloads

458

Readme

Create type-safe schemas for ws message events. Easily construct and handle ws message content, with type-safety and inferrance for payloads specific to each defined event.

🏠 Homepage

Install

npm i ws-schema zod

Usage

Defining a schema

Define a schema using the WsSchema class.

Here, the keys represent the name of each event you which to define in your schema. The value pairs then define the structure of the payload associated to that event, using the zod library.

import { WsSchema } from "ws-schema";
import { z } from "zod";

const wsSchema = new WsSchema({
  heartbeat: z.object({}),
  message: z.string(),
  user_online: z.object({
    user: z.object({
      name: z.string(),
      id: z.number(),
      bio: z.string().optional(),
    }),
    isfriend: z.boolean(),
  }),
});

Constructing / sending an event

Using the schema you have now defined, you may now easily construct and send message payloads with full type inferrence and safety.

// Define the payload for one of the events defined in our schema
const hello = wsSchema.send("message").data("hello world");

// Select how we want to use this constructed event

// JSON stringified
hello.stringify();

// js object
hello.object();

// Send directly to some websocket(s)
hello.to(someWebsocket, anotherWebsocket).emit();

or

// Or even just construct and send an event in one statement
wsSchema.send("message").data("this library is awesome").to(someWebsocket).emit();

Constructing a receiver callback

Of course if you are sending these events, you must have some logic on the other side that will handle these events in some way when they are received.

This is easily done using the receiver method, in which we can optionally define callbacks for each event, the payload attached to the given event will be given as the argument for the callback.

Once again, the payloads and events come with full type inferrence & safety.

Run-time validation occurs on incoming payloads for each event, to ensure the payload is of a valid structure for the event as defined within the schema. The callback will only be called if this validation is successful.

// Define our callbacks
const onIncomingMessage = wsSchema.receiver({
  message: (message) => {
    console.log("received a new message!", message);
  },
  user_online: ({ isfriend, user }) => {
    if (isfriend) console.log("friend online!", user.name);
  },
});
// Assuming ws is our websocket

// Define a callback for a ws message event
// (this is not the same as our custom defined 'message' event in our schema, but instead a standardised event for websockets)

ws.on("message", (incomingMessage) => {
  // pass the string representation of our incoming message to the callback we created with our schema
  onIncomingMessage(incomingMessage.data);
});

Author

👤 flynnhillier

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2024 flynnhillier. This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator