2.2.18 โ€ข Published 7 months ago

@yomo/presence v2.2.18

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

๐Ÿงฌ Introduction

npm.io

Presencejs is a JavaScript library that enables the creation of real-time web applications with a secure, low-latency, and high-performance geo-distributed architecture.

Key Features:

  • Geo-distributed Architecture: Deploy your real-time backend close to users all over the world for better performance.
  • WebTransport Support: WebTransport is an new API that offers low-latency, bidirectional, client-server messaging.
  • Secure, low-latency, and high-performance: PresenceJS prioritizes security, speed, and performance for a seamless user experience.
  • Real-time and collaborative experience: With PresenceJS, components receive data flow in real time, ensuring fast and reactive UI by offering the flexibility to send either unreliable or reliable data
  • Easy to use: PresenceJS is simple to implement, making it an accessible solution for developers.
  • Free for self-managed hosting: PresenceJS is free to use for self-managed hosting, making it an affordable choice for projects of any size.

๐ŸŒŸ Showcase

These React Serverless Components are built with presencejs:

๐Ÿ‘ฏโ€โ™€๏ธ GroupHug

Live collaborator avatars for multiplayer web apps

๐Ÿฅท๐Ÿผ Quick Start

1. Add presencejs to your web app

Using npm

$ npm i --save @yomo/presence

Using bun.js

$ bun add @yomo/presence

Using pnpm

$ pnpm i @yomo/presence

Create a Presence instance

import Presence from "@yomo/presence";

// create an instance.
const p = new Presence("https://prsc.yomo.dev", {
  url: process.env.NEXT_PUBLIC_PRESENCE_URL,
  publicKey: process.env.NEXT_PUBLIC_PRESENCE_PUBLIC_KEY,
  id,
  appId: process.env.NEXT_PUBLIC_APP_ID,
  debug: true,
});

p.on("connected", () => {
  console.log("Connected to server: ", p.host);
});

Create Channel

add subscribe to peers online event:

const c = p.joinChannel('group-hug', myState);

c.subscribePeers((peers) => {
    peers.forEach((peer) => {
      console.log(peer + " is online")
    }
});

Broadcast messages to all peers in this channel

const cb = () => {
  const state = document.hidden ? "away" : "online";
  c.broadcast("hidden-state-change", { state });
};
document.addEventListener("visibilitychange", cb);

Subscribe messages from the other peers

const unsubscribe = channel.subscribe(
  "hidden-state-change",
  ({ payload, peerState }) => {
    console.log(`${peerState.id} change visibility to: ${payload}`);
  },
);

2. Start prscd backend service

see prscd

๐Ÿคน๐Ÿปโ€โ™€๏ธ API

Presence

  • joinChannel: return a Channel object

Channel

  • subscribePeers: observe peers online and offline events.
  • broadcast: broadcast events to all other peers.
  • subscribe: observe events indicated
  • leave: leave from a Channel

๐Ÿก Self-managed hosting

Tutorial: Single node on Digital Ocean

Tutorial: Geo-distributed system on AWS

Tutorial: Geo-distributed system on Azure

License

The MIT License.

2.2.17

7 months ago

2.2.1

10 months ago

2.2.18

7 months ago

2.2.15

7 months ago

2.2.3

10 months ago

2.2.16

7 months ago

2.2.2

10 months ago

2.2.13

8 months ago

2.2.5

10 months ago

2.2.14

8 months ago

2.2.4

10 months ago

2.2.11

8 months ago

2.2.12

8 months ago

2.2.6

10 months ago

2.2.10

8 months ago

2.2.9

9 months ago

2.2.8

10 months ago

2.2.0

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.3

1 year ago

2.1.0-alpha1

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago