wdjopa / partykit

Everything's better with friends.

Home Page:https://partykit.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PartyKit

npm beta Discord License

partykit is an SDK designed for creating real-time collaborative applications.

Whether you wish to augment your existing web applications or construct new ones from scratch, partykit makes the task easier with minimal coding effort.

⚠️ Please note, all updates to partykit are currently published directly to npm using the beta tag.

Installation

Install partykit through npm:

npm install partykit@beta y-partykit@beta

For yarn users:

yarn add partykit@beta y-partykit@beta

Quick Start

The fundamental components of a partykit application are the server and the client. The server is a simple JavaScript module exporting an object that defines how your server behaves, primarily in response to WebSocket events. The client connects to this server and listens for these events.

For a quick demonstration, we will create a server that sends a welcome message to the client upon connection to a room. Then, we will set up a client to listen for this message.

First, let's create our server:

// server.ts
export default {
  onConnect(websocket, room) {
    // This is invoked whenever a user joins a room
    websocket.send("hello from room: " + room.id);
  },
};

To start the server for local development, run:

npx partykit dev server.ts

When you're ready to go live, deploy your application to the cloud using:

npx partykit deploy server.ts --name my-party

Next, connect your application to this server with a simple client:

// Import PartySocket - a lightweight abstraction over WebSocket 
import PartySocket from "partysocket";

const socket = new PartySocket({
  host: "localhost:1999", // for local development
  // host: "my-party.username.partykit.dev", // for production
  room: "my-room",
});

socket.on("message", (message) => {
  console.log(message); // "hello from room: my-room"
});

Libraries

y-partykit

y-partykit is an addon library for partykit designed to host backends for Yjs, a high-performance library of data structures for building collaborative software. You can set up a Yjs backend with just a few lines of code:

// server.ts
import { onConnect } from "y-partykit";

export default { onConnect };

Then, use the provider to connect to this server:

import YPartyKitProvider from "y-partykit/provider";

const provider = new YPartyKitProvider("localhost:1999", "my-room", doc);

Refer to the official Yjs documentation for more information. Examples provided in the Yjs documentation should work seamlessly with y-partykit (ensure to replace y-websocket with y-partykit/provider).

Contributing

We encourage contributions to partykit. If you're interested in contributing or need help or have questions, please join us in our Discord.

License

partykit is MIT licensed.

About

Everything's better with friends.

https://partykit.io/

License:MIT License


Languages

Language:TypeScript 95.3%Language:JavaScript 4.6%Language:HTML 0.1%