openrtc-netcode 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,92 @@
1
+ Required Notice: Copyright (c) 2026 Bryant Hargreaves and OpenRTC contributors.
2
+ Licensor Line of Business: OpenRTC realtime collaboration runtime, SDKs, developer services, and related tools (https://openrtc.app)
3
+
4
+ # PolyForm Shield License 1.0.0
5
+
6
+ <https://polyformproject.org/licenses/shield/1.0.0>
7
+
8
+ ## Acceptance
9
+
10
+ In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
11
+
12
+ ## Copyright License
13
+
14
+ The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
15
+
16
+ ## Distribution License
17
+
18
+ The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
19
+
20
+ ## Notices
21
+
22
+ You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
23
+
24
+ > Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
25
+
26
+ ## Changes and New Works License
27
+
28
+ The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
29
+
30
+ ## Patent License
31
+
32
+ The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
33
+
34
+ ## Noncompete
35
+
36
+ Any purpose is a permitted purpose, except for providing any product that competes with the software or any product the licensor or any of its affiliates provides using the software.
37
+
38
+ ## Competition
39
+
40
+ Goods and services compete even when they provide functionality through different kinds of interfaces or for different technical platforms. Applications can compete with services, libraries with plugins, frameworks with development tools, and so on, even if they're written in different programming languages or for different computer architectures. Goods and services compete even when provided free of charge. If you market a product as a practical substitute for the software or another product, it definitely competes.
41
+
42
+ ## New Products
43
+
44
+ If you are using the software to provide a product that does not compete, but the licensor or any of its affiliates brings your product into competition by providing a new version of the software or another product using the software, you may continue using versions of the software available under these terms beforehand to provide your competing product, but not any later versions.
45
+
46
+ ## Discontinued Products
47
+
48
+ You may begin using the software to compete with a product or service that the licensor or any of its affiliates has stopped providing, unless the licensor includes a plain-text line beginning with `Licensor Line of Business:` with the software that mentions that line of business. For example:
49
+
50
+ > Licensor Line of Business: YoyodyneCMS Content Management System (http://example.com/cms)
51
+
52
+ ## Sales of Business
53
+
54
+ If the licensor or any of its affiliates sells a line of business developing the software or using the software to provide a product, the buyer can also enforce [Noncompete](#noncompete) for that product.
55
+
56
+ ## Fair Use
57
+
58
+ You may have "fair use" rights for the software under the law. These terms do not limit them.
59
+
60
+ ## No Other Rights
61
+
62
+ These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
63
+
64
+ ## Patent Defense
65
+
66
+ If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
67
+
68
+ ## Violations
69
+
70
+ The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
71
+
72
+ ## No Liability
73
+
74
+ ***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
75
+
76
+ ## Definitions
77
+
78
+ The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
79
+
80
+ A **product** can be a good or service, or a combination of them.
81
+
82
+ **You** refers to the individual or entity agreeing to these terms.
83
+
84
+ **Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all its affiliates.
85
+
86
+ **Affiliates** means the other organizations than an organization has control over, is under the control of, or is under common control with.
87
+
88
+ **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
89
+
90
+ **Your licenses** are all the licenses granted to you for the software under these terms.
91
+
92
+ **Use** means anything you do with the software requiring one of your licenses.
package/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # openrtc-netcode
2
+
3
+ `openrtc-netcode` is a small multiplayer layer on top of OpenRTC. It treats
4
+ OpenRTC rooms as lobbies, forms a deterministic P2P mesh between lobby members,
5
+ and exposes reliable typed messages, lightweight replicated state, networked
6
+ objects, and browser voice-chat signaling.
7
+
8
+ Gameplay payloads are sent over OpenRTC peer data paths only: Iroh
9
+ relay/direct, WebRTC DataChannel upgrades, and MoQ routes when enabled. Firebase
10
+ and Firestore remain part of OpenRTC's control plane for identity, discovery,
11
+ rooms, tickets, and browser signaling metadata; `openrtc-netcode` does not use
12
+ Firestore as a state or message relay.
13
+
14
+ ```ts
15
+ import { createNetcode } from 'openrtc-netcode';
16
+
17
+ const netcode = createNetcode({ apiKey: 'pk_live_...' });
18
+ await netcode.createLobby({ id: 'duel-room' });
19
+
20
+ netcode.onMessage('chat', (message) => {
21
+ console.log(message.from, message.payload);
22
+ });
23
+
24
+ await netcode.broadcast('chat', { text: 'hello' });
25
+ ```
26
+
27
+ Networked game objects are synchronized through `netcode.objects`:
28
+
29
+ ```ts
30
+ await netcode.objects.spawn({
31
+ id: 'crate-1',
32
+ kind: 'crate',
33
+ transform: { position: { x: 0, y: 1, z: 0 } },
34
+ physics: { linearVelocity: { x: 0, y: 0, z: 0 } },
35
+ });
36
+ ```
37
+
38
+ Voice chat uses OpenRTC netcode connections for SDP/ICE signaling and lets the
39
+ browser WebRTC stack carry microphone media:
40
+
41
+ ```ts
42
+ await netcode.voice.start();
43
+ netcode.voice.on('peer:stream', ({ stream }) => {
44
+ audioElement.srcObject = stream;
45
+ });
46
+ ```
@@ -0,0 +1,112 @@
1
+ import { ReplicatedObjectStore } from './objects.js';
2
+ import { ReplicatedState } from './state.js';
3
+ import { NetcodeVoiceController } from './voice.js';
4
+ import type { CreateLobbyOptions, JoinLobbyOptions, NetcodeClient as NetcodeClientInterface, NetcodeEventHandler, NetcodeEventName, NetcodeLobby, NetcodeMessageHandler, NetcodeOptions, NetcodePeer, NetcodePeerStats, SendOptions } from './types.js';
5
+ export declare class OpenRtcNetcodeClient implements NetcodeClientInterface {
6
+ private readonly options;
7
+ private runtime;
8
+ private ownsRuntime;
9
+ private localPeerId;
10
+ private currentLobby;
11
+ private currentRoomId;
12
+ private fallbackChannel;
13
+ private lobbyMetadata;
14
+ private memberMetadata;
15
+ private seq;
16
+ private started;
17
+ private stopped;
18
+ private stopWatchingRoom;
19
+ private stopRuntimeConnections;
20
+ private stopIncomingNetcodeChannel;
21
+ private peerPingTimer;
22
+ private readonly emitter;
23
+ private readonly messageListeners;
24
+ private readonly peersById;
25
+ private readonly peerStatsById;
26
+ private readonly connectionsByPeer;
27
+ private readonly channelsByPeer;
28
+ private readonly pendingChannelsByPeer;
29
+ private readonly connectionIds;
30
+ private readonly pendingPeerIds;
31
+ private readonly directSendWarnings;
32
+ private readonly requestedWebRtcUpgradeConnectionIds;
33
+ private readonly introPeerIds;
34
+ private readonly seenEnvelopeIds;
35
+ private readonly seenEnvelopeOrder;
36
+ private readonly stateStore;
37
+ private readonly objectStore;
38
+ private readonly voiceController;
39
+ constructor(options?: NetcodeOptions);
40
+ get peerId(): string | null;
41
+ get lobby(): NetcodeLobby | null;
42
+ get peers(): NetcodePeer[];
43
+ get peerStats(): NetcodePeerStats[];
44
+ get state(): ReplicatedState;
45
+ get objects(): ReplicatedObjectStore;
46
+ get voice(): NetcodeVoiceController;
47
+ start(): Promise<void>;
48
+ stop(): Promise<void>;
49
+ createLobby(options?: CreateLobbyOptions): Promise<NetcodeLobby>;
50
+ joinLobby(lobbyId: string, options?: JoinLobbyOptions): Promise<NetcodeLobby>;
51
+ joinOrCreateLobby(lobbyId: string, options?: JoinLobbyOptions): Promise<NetcodeLobby>;
52
+ private activateLobby;
53
+ private joinLobbyWithRetry;
54
+ private getRoomMembersWithRetry;
55
+ leaveLobby(): Promise<void>;
56
+ send(peerId: string, type: string, payload: unknown, options?: SendOptions): Promise<void>;
57
+ broadcast(type: string, payload: unknown, options?: SendOptions): Promise<void>;
58
+ on<K extends NetcodeEventName>(event: K, handler: NetcodeEventHandler<K>): () => void;
59
+ onMessage<T = unknown>(type: string, handler: NetcodeMessageHandler<T>): () => void;
60
+ setLobbyData(key: string, value: string): Promise<void>;
61
+ setMemberData(key: string, value: string): Promise<void>;
62
+ private handleRoomMembers;
63
+ private reconcileMesh;
64
+ private attachConnection;
65
+ private sendPeerIntro;
66
+ private queuePeerIntro;
67
+ private handleConnectionMessage;
68
+ private markEnvelopeSeen;
69
+ private handleHello;
70
+ private handleUserMessage;
71
+ private handleStateSnapshot;
72
+ private handleStatePatch;
73
+ private handleObjectSnapshot;
74
+ private handleObjectUpsert;
75
+ private handleObjectRemove;
76
+ private handleLobbyMetadata;
77
+ private handleMemberMetadata;
78
+ private handleVoiceSignal;
79
+ private handlePong;
80
+ private sendStateSnapshot;
81
+ private sendObjectSnapshot;
82
+ private broadcastMemberMetadata;
83
+ private startPeerPingLoop;
84
+ private stopPeerPingLoop;
85
+ private broadcastEnvelope;
86
+ private sendEnvelope;
87
+ private sendDirectEnvelope;
88
+ private warnDirectSendFailure;
89
+ private openFallbackChannel;
90
+ private closeFallbackChannel;
91
+ private resolveNetcodeChannel;
92
+ private resolvePeerTicket;
93
+ private attachNetcodeChannel;
94
+ private registerIncomingNetcodeChannel;
95
+ private readIncomingNetcodeStream;
96
+ private closeNetcodeChannels;
97
+ private createEnvelope;
98
+ private resolveStateOwner;
99
+ private upsertPeer;
100
+ private updatePeerStats;
101
+ private resolvePeerTransport;
102
+ private isPeerPayloadReady;
103
+ private resolvePayloadConnection;
104
+ private requestPreferredTransportUpgrade;
105
+ private payloadReadinessOptions;
106
+ private refreshLobby;
107
+ private requireRuntime;
108
+ private requireLobby;
109
+ private registerNetcodeChannel;
110
+ }
111
+ export declare function createNetcode(options?: NetcodeOptions): OpenRtcNetcodeClient;
112
+ export declare function shouldInitiateConnection(localPeerId: string, remotePeerId: string): boolean;