Coder Social home page Coder Social logo

react-declarative / nestjs-appwrite-bff Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 3.0 130 KB

Long polling realtime event support for AppWrite

Home Page: https://www.npmjs.com/package/cra-template-appwrite

License: MIT License

JavaScript 8.67% TypeScript 91.33%
appwrite longpolling realtime sockjs

nestjs-appwrite-bff's Introduction

nestjs-appwrite-bff

AppWrite PWA bootstrap starter kit

Appwrite Cloud Card

The problem

Currently AppWrite does not support Long Polling instead of WebSocket. This is critical if you want to use Ngrok. Also, using additional microservices together with the AppWrite functions is a must have feature for API integration to other systems: What if we need to cache some data in Redis before writing It into the production database?

Solution

You can use createProxyMiddleware with Express web server to connect your PWA with backend microservices. Also this tool restream AppWrite Realtime events by using sockjs-client

import AppwriteService from "./AppwriteService";
import { CC_APPWRITE_EVENT_RESTREAMER_URL } from "../../../config/params";
import Socket from "sockjs-client";
import TYPES from "../../types";
import { inject } from "react-declarative";
import { makeObservable } from "mobx";

type RealtimeResponseEvent<T extends unknown> = {
  events: string[];
  channels: string[];
  timestamp: number;
  payload: T;
};

export class RealtimeService {
  readonly appwriteService = inject<AppwriteService>(TYPES.appwriteService);

  constructor() {
    makeObservable(this, {});
  }

  subscribe = <T extends unknown>(
    channels: string | string[],
    callback: (payload: RealtimeResponseEvent<T>) => void
  ) => {
    if (CC_APPWRITE_EVENT_RESTREAMER_URL) {
      const socket = new Socket(CC_APPWRITE_EVENT_RESTREAMER_URL);

      socket.onopen = () => {
        socket.send(JSON.stringify(channels));
      };

      socket.onerror = (error) => {
        console.error(error);
      };

      socket.onmessage = (msg) => {
        const payload = JSON.parse(msg.data);
        callback(payload);
      };

      socket.onclose = (...args) => {
        console.log("socket closed", { args });
      };

      return () => socket.close();
    }
    return this.appwriteService.client.subscribe(channels, callback);
  };
}

export default RealtimeService;

Just copy and paste that code to cra-template-appwrite and enjoy long polling appwrite events

nestjs-appwrite-bff's People

Contributors

tripolskypetr avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.