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

unexpected-realtime

v0.0.15

Published

<div align="center" > <h3>Realtime client for <a href="https://unexpected.app" style="display: inline-flex; align-items: center; gap: 4px"> Unexpected Cloud <svg xmlns="http://www.w3.org/2000/svg" width="24px" height="24px" viewBox="0 0 24

Downloads

293

Readme

What is realtime?

The real-time web is a network web using technologies and practices that enable users to receive information as soon as it is published by its authors, rather than requiring that they or their software check a source periodically for updates.

How it works?

Unexpected Cloud offers the capability for the server to notify clients about changes. The platform creates a worker with a WebSocket server and establishes a WebSocket connection. To monitor changes to a specific entity within a single WebSocket connection, the platform utilizes channels. To notify the WebSocket server about changes, simply make an HTTP request and specify the relevant channels. Once the server is notified of a change in a channel, it will send a ping message to all clients subscribed to that channel.

Getting Started

  1. Create project on Unexpected Cloud.
  2. Install NPM package.
npm i unexpected-realtime

projectId can be found in the project menu in the Details tab or in the Realtime tab within the connection code snippet.

Pings

Server

Ping the client from the server using POST request with Fetch API or with Unexpected Cloud Custom Worker.

secretKey can be found in the project menu in the Details tab or in the Realtime tab within the connection code snippet.

// env.REALTIME_WORKER.fetch for Custom Workers
fetch(
  `https://unexpected-realtime-${projectId}.lunaxodd.workers.dev/ping`,
  {
    method: 'POST',
    body: JSON.stringify({
      secret: secretKey,
      channels: ['channel']
    })
  }
);

Client

Pings API allows to notify clients about changes.

import PingsClient from "unexpected-realtime/pings";

// Instance declaration of PingsClient, creates connection to realtime server.
const pingsClient = new PingsClient(projectId);

RealtimeClient

| Method | Description | Arguments | | :------------ | :-------------------------------- | :--------------------- | | subscribe | Subscribe to specific channel | channelName, handler | | unsubscribe | Unsubscribe from specific channel | channelName |

Live Query

Live Query API allows clients to retrieve data from the server securely when it changes.

Server

import { createQuery, setupLive } from "unexpected-realtime/live-query/server";

export const searchPosts = createQuery("posts", (qb, auth, params) => {
  const conditions = [];

  if (params.lastId) {
    conditions.push({
      column: "rowid",
      [params.order === "asc" ? "gt" : "lt"]: params.lastId,
    });
  }

  return qb
    .select("posts")
    .where({
      and: conditions,
    })
    .limit(params.limit)
    .order(params.order);
});

export const getAuthContext = async (env, data) => {
  return {
    id: 1,
    username: "test",
  };
};

export default setupLive({
  getAuthContext,
  queries: {
    searchPosts,
  },
});

createQuery arguments:

  1. qb (Query builder) builds the query.
  2. auth (Auth context) contains the data returned from the getAuthContext function. Allows to filter data for specific users in query builder.
  3. params (Query parameters) includes essential information such as order and limit. It can also contain custom parameters specific to your query needs.

Deployment

  1. Install unexpected-cli-sandbox:
npm i -g unexpected-cli-sandbox
  1. Create table trigger for posts table:
unexpected-cli-sandbox set-trigger --table posts
  1. Deploy Live Query Worker:
unexpected-cli-sandbox deploy-live-query

Client

import LiveQueryClient from "unexpected-realtime/live-query/client";

// Instance declaration of LiveQueryClient, creates connection to live server.
const liveQueryClient = new LiveQueryClient('https://...');

LiveQueryClient

| Method | Description | Arguments | | :------------ | :--------------------------------- | :------------------ | | subscribe | Subscribe to specific query | queryName, params | | unsubscribe | Unsubscribe from specific query | queryName | | next | Fetches the next page of the query | queryName |