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

@hyper-graph/ws-adapter

v0.7.0

Published

Websocket adapter for nestjs framework

Downloads

6

Readme

NestJS Websocket Adapter

Motivation:

Websocket adapters, which provided by NestJS, are very simple and cannot be used in large applications. They can only receive messages, send replies, without correlation id, and send events. Using them you can't reply with correlation id in common, can't verify connect and etc. This adapter try to resolve more of common cases.

Features

Correlation ID

Then you return data from a gateway it will return with correlationId which was received from client. This behavior can be configured by adapter options.

Events

It's not a feature, but events has different api than nestjs adapters. You can emit an event on connect and all data that you pass will be sent to client with wrapping into protocol.

Formatter

You can specify a formatter for your application. It means you can use JSON, yml, xml, binary or another format of data.

Protocol

Protocol control all data that receive or send application and wrap it into control object. Protocol can receive correlationId and send it in response with data what return message handler. You can also pass your own implementation of protocol.

Connect verification

You can set connect verification handler. This handler will be called for each connect request. If handler throws an error, connect will be rejected. You can set headers that will be sent to client with accept message.

Multiple handlers

You can set as many handlers for one patter as you need to. All responses will be merged into one object and pass into protocol as one result.

Regex patterns

In nestjs adapters you can pass only string as pattern into SubscribeMessage decorator. All messages checks using strict equal. This adapter supports string and regex for searching handlers which will be called.

Gateway namespaces

You can specify namespaces for your gateway. How do they work depends on protocol. See more information in API Reference.

API Reference:

Adapter

Base usage:

const app = await NestFactory.create(AppModule);

app.useWebsocketAdapter(new WebsocketAdapter(app));

await app.listenAsync(3000);

Formatter

You can pass formatter in options in first argument.

const app = await NestFactory.create(AppModule);

app.useWebsocketAdapter(new WebsocketAdapter(app, {
    formatter: new JsonFormat(),
}));

await app.listenAsync(3000);

JsonFormat is builtin formatter.

You can write your own formatter for example for yml.

For do this you will extend BaseFormat class and implement to methods: parse and format.

Protocol

You can use protocol like formatter. It can be passed in options in second argument.

const app = await NestFactory.create(AppModule);

app.useWebsocketAdapter(new WebsocketAdapter(app, {
    protocol: new JsonRpcProtocol(),
}));

await app.listenAsync(3000);

JsonRpcProtocol is builtin protocol.

You can write your own protocol handler.

For do this you will extend BaseProtocol class and implement to methods: handle and formatEvent.

These methods are not so simple as parse and format in a formatter, but BaseProtocol class provides some helpful methods. You also can use JsonRpcProtocol class as example for your own implementation.

Namespace delimiter

If you want to use namespaces, you can specify a delimiter between namespaces and method.

const app = await NestFactory.create(AppModule);

app.useWebsocketAdapter(new WebsocketAdapter(app, {
    namespaceDelimiter: '/',
}));

await app.listenAsync(3000);

Gateways

WebSocketGateway Decorator

This adapter support WebSocketGateway arguments. NestJS use first argument as port if it's a number else it will be used as options. If you pass a port, you can pass options in second argument.

TODO: Work In Progress