GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - lambda-direct/unexpected-realtime

BranchesTags
Open more actions menu

Latest commit

 

History

53 Commits
53 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
scripts
scripts
 
 
src
src
 
 
.gitignore
.gitignore
 
 
README.md
README.md
 
 
biome.json
biome.json
 
 
package.json
package.json
 
 
tsconfig.build.json
tsconfig.build.json
 
 
tsconfig.dts.json
tsconfig.dts.json
 
 
tsconfig.json
tsconfig.json
 
 

Repository files navigation

Realtime client for Unexpected Cloud

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

About

No description, website, or topics provided.

Resources

Readme
Activity
Custom properties

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.