Coder Social home page Coder Social logo

partykit's Introduction

(All commits get published to npm with a beta tag, so use that when calling npm install or npx. Example: npm install partykit@beta y-partykit@beta. Join us at our discord https://discord.gg/vx4BmTfd)

partykit

Everything's better with friends.

partykit is a software development kit for building realtime collaborative applications. You can use it with your existing web applications, or build new ones from scratch.

You can create a server with very little code:

// server.ts

export default {
  onConnect(websocket, room) {
    // called whenever a user join a room
    websocket.send("hello from room: " + room.id);
  },
};

Then run npx partykit dev server.ts to start the server for local development. You can later deploy it to the cloud with npx partykit deploy server.ts --name my-party.

Then, in your application, you can connect to this server with a simple client:

// PartySocket is a small abstraction over
// WebSocket that adds reconnection logic, etc.
import PartySocket from "partysocket";

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

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

This way, you can add realtime collaboration to your existing web application with very little code. It runs alongside your existing application, and you can use it to build realtime features like collaborative text editors, multiplayer games, and more.

y-partykit

Yjs is a library of data structures for building collaborative applications. y-partykit is a library that makes it easy to host backends for Yjs on partykit. You can create a yjs backend with as little code as this:

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

export default { onConnect };

Then, you can use the provider to connect to this server:

import YPartyKitProvider from "y-partykit/provider";

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

Have a look at the official documentation, their examples should just work with y-partykit (replacing y-websocket with y-partykit/provider like above)

party.io

(NB: THIS DOES NOT EXIST YET)

party.io is a library heavily influenced by socket.io. It's an abstraction over partysocket that makes it easy to build realtime applications. You might use it on the client:

import Party from "party.io";

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

const socket = io.join("my-room");

socket.emit("hello", "world"); // named events

socket.emit("hello", { nested: { object: "world" } }, ["some", "array"]); // nested objects and arrays

socket.emit("hello", [1, 2, 3], (...args) => {
  // callbacks
});

socket.on("hello", (arg1, arg2, callback) => {
  // subscribe to events
});

And on the server:

import { onConnect } from "party.io";

export default {
  onConnect(ws, room) {
    onConnect(ws, room, (socket) => {
      socket.on("hello", (arg1, arg2, callback) => {
        // subscribe to events
      });

      socket.emit("hello", "world"); // named events
      // ...and so on
    });
  },
};

partykit's People

Contributors

anmolm96 avatar aweary avatar jevakallio avatar joshuakgoldberg avatar mellson avatar threepointone avatar trysound avatar

Watchers

 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.