
💃 Getting Started ​
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
bash
$ npm i @jolly-pixel/network
# or
$ yarn add @jolly-pixel/network👀 Usage example ​
Vite server ​
ts
import * as network from "@jolly-pixel/network";
import {
createWebSocketNetworkPlugin
} from "@jolly-pixel/network/node";
export default {
plugins: [
createWebSocketNetworkPlugin({
extensions: [
new network.PresenceOnlyExtension("echo", "echo", {
broadcast: true
})
]
})
]
};Browser client ​
ts
import * as network from "@jolly-pixel/network/client";
const client = new network.Client();
const room = client.room("echo");
room.on("message", (payload) => console.log(payload));
room.on("peer-joined", (event) => console.log(`${event.clientId} joined`));
room.on("peer-left", (event) => console.log(`${event.clientId} left`));
room.join();
room.send({ hello: "world" });📖 Guides ​
- Getting started: host a room on Vite and exchange messages between tabs
- Command synchronization: sync a command document and the server's part in it
📚 API ​
Client ​
Import from @jolly-pixel/network/client.
- Client:
Client,Room,PresenceChannel,RoomGrants - CommandSync: document synchronization and adapters
- connectWebSocket: browser socket factory
Server ​
Import from @jolly-pixel/network; Node.js hosting comes from @jolly-pixel/network/node.
- Server: connections and room lifecycle
- Extension: feature hooks, presence-only rooms, workers
- Access: authentication and rights
- Arbitration:
KeyedCommandArbiter - WebsocketTransport: WebSocket hosting and the Vite plugin
Protocol ​
Shared by both sides.
- Messages: message protocols, command schemas, parsing
- Peer profile: profile fields and parsing
In-process transports ​
- LoopbackTransport and channels: tests, workers, and WebRTC data channels
📈 Benchmarks ​
bash
$ pnpm --filter @jolly-pixel/network bench
$ pnpm --filter @jolly-pixel/network bench:load --scenario command --clients 150bench runs the micro suites in bench/*.bench.ts (--allocations adds B/op). bench:load drives a server over real WebSockets; --help lists the scenarios and options.
✨ Contributors guide ​
Read the contributing guide before submitting a change.
Once you have finished your development, check that the tests (and linter) are still good by running the following script:
bash
$ pnpm run test
$ pnpm run lintCAUTION
In case you introduce a new feature or fix a bug, make sure to include tests for it as well.
📃 License ​
MIT