|
1 | 1 | # The Poki Networking Library [<img src="https://img.shields.io/npm/v/@poki/netlib?color=lightgray">](https://www.npmjs.com/package/@poki/netlib) |
2 | 2 |
|
3 | 3 | <img align="right" src="https://raw.githubusercontent.com/poki/netlib/main/.github/logo.png" width=140> |
4 | | -The Poki Networking Library is a peer-to-peer library for web games, using WebRTC datachannels to provide UDP connections between players directly. Like the Steam Networking Library, but for web. |
5 | | -Netlib tries to make WebRTC as simple to use as the WebSocket interface (for game development). |
| 4 | +The Poki Networking Library is a peer-to-peer networking library for web games, leveraging WebRTC datachannels to enable direct UDP connections between players. Think of it as the Steam Networking Library for the web, designed to make WebRTC as simple to use as WebSockets for game development. |
| 5 | + |
| 6 | +<p></p> |
| 7 | + |
| 8 | +> [!WARNING] |
| 9 | +> This library is still under development and considered a beta. While it's being actively used in production by some games, the API can change. Make sure to get in touch if you want to go live with this so we can keep you up-to-date about changes. |
| 10 | +
|
| 11 | +## Features |
| 12 | + |
| 13 | +- **True Peer-to-Peer (P2P) Networking** |
| 14 | + - Direct client-to-client connections without a central game server |
| 15 | + - Lower latency for geographically close players |
| 16 | + - Reduced server costs and infrastructure complexity |
| 17 | + - No need to duplicate game logic between client and server |
| 18 | + - Three main advantages: |
| 19 | + 1. No server costs - there is no server running the game |
| 20 | + 2. No double implementation - you don't need to write your game logic twice (for the client and the server) |
| 21 | + 3. Lower latency - when players are living close by, the latency is often much lower than when connected via a server |
| 22 | + |
| 23 | +- **UDP Performance** |
| 24 | + - Choice between reliable (TCP) and unreliable (UDP) channels |
| 25 | + - Optimized for real-time gaming with minimal latency |
| 26 | + - Perfect for fast-paced multiplayer games |
| 27 | + - Unlike WebSockets or HTTP (which use TCP), UDP doesn't pause new packets when one packet is slow or dropped |
| 28 | + - Includes reliable data channels for critical events like chat messages or NPC spawns |
| 29 | + |
| 30 | +- **Easy to Use** |
| 31 | + - Simple WebSocket-like API |
| 32 | + - Built-in lobby system with filtering |
| 33 | + - Automatic connection management |
| 34 | + - Comprehensive TypeScript support |
| 35 | + |
| 36 | +- **Production Ready** |
| 37 | + - Fallback to TURN servers when direct P2P fails |
| 38 | + - Built-in connection quality monitoring |
| 39 | + - Automatic reconnection handling |
| 40 | + - Secure by default |
| 41 | + |
| 42 | +## Quick Start |
| 43 | + |
| 44 | +1. Install the package: |
| 45 | +```sh |
| 46 | +yarn add @poki/netlib |
| 47 | +# or |
| 48 | +npm install @poki/netlib |
| 49 | +``` |
6 | 50 |
|
| 51 | +2. Create a network instance: |
| 52 | +```js |
| 53 | +import { Network } from '@poki/netlib' |
| 54 | +const network = new Network('<your-game-id>') |
| 55 | +``` |
7 | 56 |
|
8 | | -## Project Status: alpha |
| 57 | +3. Create or join a lobby: |
| 58 | +```js |
| 59 | +// Create a new lobby |
| 60 | +network.on('ready', () => { |
| 61 | + network.create() |
| 62 | +}) |
| 63 | + |
| 64 | +// Or join an existing one |
| 65 | +network.on('ready', () => { |
| 66 | + network.join('ed84') |
| 67 | +}) |
| 68 | +``` |
9 | 69 |
|
10 | | -This library is still under heavy development and considered in alpha. The library API can and will change without warning and without bumping the major version. Make sure to get in touch if you want to go live with this netlib. |
| 70 | +4. Start communicating: |
| 71 | +```js |
| 72 | +// Send messages |
| 73 | +network.broadcast('unreliable', { x: 100, y: 200 }) |
11 | 74 |
|
12 | | -One missing feature that is next on the roadmap is lobby listing and discovery. Currently you can only connect peers by having players share a lobby code externally. |
| 75 | +// Receive messages |
| 76 | +network.on('message', (peer, channel, data) => { |
| 77 | + console.log(`Received from ${peer.id}:`, data) |
| 78 | +}) |
| 79 | +``` |
13 | 80 |
|
14 | | -## Library main advantages: |
| 81 | +For more detailed examples and API documentation: |
| 82 | +- [Basic Usage Guide](./docs/basic-usage.md) |
| 83 | +- [Example Usage](./example/) |
15 | 84 |
|
16 | | -- **peer-to-peer (p2p)** |
17 | | - Clients connected to each other using this netlib will be connected directly without a central server in between (unless using the fallback TURN server). This has three main advantages: |
18 | | - 1. No server costs, there is no server running the game. |
19 | | - 1. No double implementation of the game. You don't need to write your game logic twice (for the client and the server). |
20 | | - 1. When players are living close by the latency is often a lot lower than when connected via a server |
21 | | -- **UDP** |
22 | | - Most web games rely on WebSockets or HTTP for communication which is always a TCP connection, but for realtime multiplayer games the UDP protocol is preferred. The main reason is: |
23 | | - When one packet is slow or dropped UDP doesn't pause new, already received, packets, this is great for things like position updates. The Poki netlib also supplies a reliable data channel useful for chat or npc spawn events. |
| 85 | +## Roadmap |
24 | 86 |
|
| 87 | +- [x] Basic P2P connectivity |
| 88 | +- [x] Lobby system |
| 89 | +- [x] Lobby discovery and filtering |
| 90 | +- [ ] WebRTC data compression |
| 91 | +- [ ] Connection statistics and debugging tools |
| 92 | +- [ ] More extensive documentation |
| 93 | +- [ ] API stability |
25 | 94 |
|
26 | | -## Setup |
| 95 | +## Architecture |
27 | 96 |
|
28 | | -First add [`@poki/netlib`](https://www.npmjs.com/package/@poki/netlib) as a dependency to your project: |
29 | | -```sh |
30 | | -# either using yarn: |
31 | | -yarn add @poki/netlib |
32 | | -# or using npm: |
33 | | -npm i @poki/netlib |
| 97 | +### Network Stack |
34 | 98 | ``` |
35 | | -Then you can import and create a _Network_ interface: |
36 | | -```js |
37 | | -import { Network } from '@poki/netlib' |
38 | | -const network = new Network('<your-game-id-here>') |
| 99 | +Your Game |
| 100 | + ↓ |
| 101 | +Netlib API |
| 102 | + ↓ |
| 103 | +WebRTC DataChannels |
| 104 | + ↓ |
| 105 | +(STUN/TURN if needed) |
| 106 | + ↓ |
| 107 | +UDP Transport |
39 | 108 | ``` |
40 | | -(any random UUID is a valid game-id, but if your game is hosted at Poki you should use Poki's game-id) |
41 | 109 |
|
42 | | -Next up: read the [**basic usage**](./docs/basic-usage.md) guide and make sure to checkout the [**example code**](./example/). |
| 110 | +### Infrastructure Components |
| 111 | + |
| 112 | +#### 1. Signaling Server |
| 113 | +- Handles initial peer discovery |
| 114 | +- Manages lobby creation and joining |
| 115 | +- Facilitates WebRTC connection establishment |
| 116 | + |
| 117 | +#### 2. STUN/TURN Servers |
| 118 | +- STUN: Helps peers discover their public IP (by default Google STUN servers) |
| 119 | +- TURN: Provides fallback relay when direct P2P fails (when using the Poki hosted version, Cloudflare TURN servers are used) |
| 120 | + |
| 121 | +## Self-Hosting |
| 122 | + |
| 123 | +While Poki provides hosted STUN/TURN and signaling services for free, you can also self-host these components: |
| 124 | + |
| 125 | +1. Set up your own signaling server using the provided Docker image |
| 126 | +2. Configure your own STUN/TURN servers |
| 127 | +3. Initialize the network with custom endpoints: |
| 128 | +```js |
| 129 | +const network = new Network('<game-id>', { |
| 130 | + signalingServer: 'wss://your-server.com', |
| 131 | + stunServer: 'stun:your-stun.com:3478', |
| 132 | + turnServer: 'turn:your-turn.com:3478' |
| 133 | +}) |
| 134 | +``` |
43 | 135 |
|
| 136 | +## Contributing |
44 | 137 |
|
45 | | -## STUN, TURN & Signaling Backend |
| 138 | +We welcome contributions! Please see our [Contributing Guide](./.github/CONTRIBUTING.md) for details. This project adheres to the [Poki Vulnerability Disclosure Policy](https://poki.com/en/c/vulnerability-disclosure-policy). |
46 | 139 |
|
47 | | -The netlib is a peer-to-peer networking library which means players are connected directly to each other and data send between them is never send to a server. |
48 | | -That said, to setup these connections we need a signaling service. This backend and STUN/TURN servers are hosted by Poki for free. You can however always decide to host the backend yourself. |
| 140 | +## License |
49 | 141 |
|
| 142 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
50 | 143 |
|
51 | 144 | ## Main Contributors |
52 | 145 |
|
|
0 commit comments