Coder Social home page Coder Social logo

yoname / presencejs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from venjiang/presencejs

0.0 0.0 0.0 2.18 MB

@yomo/presencejs ⚡️ Edge Infra for Realtime Web Applications, geo-distributed architecture.

Home Page: https://presence.js.org

License: MIT License

Shell 1.40% JavaScript 4.22% Go 45.07% TypeScript 38.51% CSS 0.29% Makefile 1.17% HTML 9.35%

presencejs's Introduction

presencejs logo
NPM version License

Cursor Chat Anywhere - Add Figma like cursor chat to your own products | Product Hunt

🧬 Introduction

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

image

🥷🏼 Quick Start

1. Add presencejs to your web app

Using npm

$ npm i --save @yomo/presence

Using yarn

$ yarn add @yomo/presence

Using pnpm

$ pnpm i @yomo/presence

Create a Presence instance

import createPresence from '@yomo/presence';

// create an instance.
const presencePromise = createPresence('https://prsc.yomo.dev', {
  publicKey: process.env.NEXT_PUBLIC_PRESENCE_PUBLIC_KEY,
  id,
  debug: true,
});

presencePromise.then((presence) => {
  console.log('Presence: ', presence);
});

// or
// (async () => {
//   const presence = await presencePromise;
// })()

Create Channel

add subscribe to peers online event:

const channel = presence.joinChannel('group-hug', myState);
// join multiple channels
// const channel2 = presence.joinChannel('live-cursor', myState);
// const channel3 = presence.joinChannel('cursor-chat', myState);

channel.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}`)
    })

unsubscribe()

2. Start prscd backend service

see prscd

🤹🏻‍♀️ API

Presence

  • joinChannel: return a Channel object

Channel

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

👩🏼‍🔬 Development

  • frontend project: pnpm i
  • local backend server: see prscd

🏡 Self-managed hosting

Docs: https://presence.js.org

License

The MIT License.

presencejs's People

Contributors

fanweixiao avatar osdodo avatar luzhenqian avatar xiaojian-hong avatar sayanta66 avatar yoname avatar imgbot[bot] 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.