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

realtime-ts

v2.2.4

Published

A realtime engine for both server and client using typescript.

Downloads

7

Readme

realtime-ts

A real time engine using websocket with node and typescript. This project also adds support for local dbs using indexedDB and synchronization with remote databases such as mongo db.

Handlers

Handlers are present on the common interface as they serve both sides. With handler you can define a name and the implementation of a method that can be called from a remote source.

Server Side handlers can be called from the client using a websocket. Something similar to a Remote Procedure Call.

Client Side handlers can be called from the server to execute actions on the client side.

There can be only one handler per name, by definition.

const handlers = new Handlers();
handlers.setHandler("ALERT_USER", (message: string) => {
  window.alert(message);
});

That can be invoked in a later part of your code. Handlers can return a value. Always wrapped in a promise.

handlers.invoke("ALERT_USER", "I'm a message to the user");

or

const r: Promise<number> = handlers.invoke("ADD", 10, 20);
r.then((result) => console.log(result));

Or it can be removed by calling:

handlers.removeHandler("ALERT_USER");

Listeners

Another common implementation on both the server and the client is the use of listeners. A listener does not returns anything. Multiple functions can listen to an event and they work very well for notification messages.

const listeners = new Listeners();
listeners.addListener("ON_NOTIFICATION", (message: string) => {
  console.log(message);
});
listeners.addListener("ON_NOTIFICATION", (message: string) => {
  window.alert(message);
});

Those methods are attached to a named event that can be called:

listeners.notify("ON_NOTIFICATION", "I'm a notification");

Listeners can be removed using the removeListener method on the class.

Those are the core concepts on both the client and the server. You'll find addHandler, addListener, removeHandler, removeListener, invoke and notify on a lot of the classes in this implementation.

The Server

The Realtime server implements the standard node http or https createServer method and we use the ws library to create a socket attached to that server.

const server = new RealtimeServer();
server.addHandler(
  "CALL_ME_MAYBE",
  (client: RealtimeServerClient, params?: any) => {
    return "Thanks for your call " + client.id;
  }
);
server.listen(1337);

The Client

The realtime client uses a browser websocket implementation to connect to a realtime server.

const client = new RealtimeClient("ws://localhost:1337");
const result: Promise<string> = client.call("CALL_ME_MAYBE");
result.then((answer) => console.log(answer));

In Between: JSON RPC

Please check the documentation for JSON RPC on this markdown documentation and feel free to collaborate and edit.

Realtime Database

Please check the documentation for the realtime database implemented using this library on this markdown documentation and feel free to collaborate and edit.