openrtc-netcode-react 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,21 @@
1
+ # openrtc-netcode-react
2
+
3
+ React context and hooks for `openrtc-netcode`.
4
+
5
+ ```tsx
6
+ import { NetcodeProvider, usePeers, useNetVar } from 'openrtc-netcode-react';
7
+
8
+ function Score() {
9
+ const peers = usePeers();
10
+ const [score, setScore] = useNetVar('score', 0);
11
+ return <button onClick={() => setScore(score + 1)}>{peers.length}: {score}</button>;
12
+ }
13
+
14
+ export function App() {
15
+ return (
16
+ <NetcodeProvider options={{ apiKey: 'pk_live_...' }}>
17
+ <Score />
18
+ </NetcodeProvider>
19
+ );
20
+ }
21
+ ```
@@ -0,0 +1,18 @@
1
+ import { type PropsWithChildren } from 'react';
2
+ import { type NetVarOptions, type NetcodeClient, type NetcodeEventName, type NetcodeEvents, type NetcodeLobby, type NetcodeMessage, type NetcodeMessageHandler, type NetcodeObjectChange, type NetcodeObjectState, type NetcodeOptions, type NetcodePeer, type NetcodePeerStats, type NetcodeVoicePeer } from 'openrtc-netcode';
3
+ export interface NetcodeProviderProps extends PropsWithChildren {
4
+ client?: NetcodeClient;
5
+ options?: NetcodeOptions;
6
+ autoStart?: boolean;
7
+ }
8
+ export declare function NetcodeProvider({ client, options, autoStart, children }: NetcodeProviderProps): import("react").FunctionComponentElement<import("react").ProviderProps<NetcodeClient | null>>;
9
+ export declare function useNetcode(): NetcodeClient;
10
+ export declare function useLobby(): NetcodeLobby | null;
11
+ export declare function usePeers(): NetcodePeer[];
12
+ export declare function usePeerStats(): NetcodePeerStats[];
13
+ export declare function useNetVar<T>(key: string, initial: T, options?: NetVarOptions): [T, (value: T) => Promise<void>];
14
+ export declare function useNetworkObjects(kind?: string): NetcodeObjectState[];
15
+ export declare function useNetcodeVoicePeers(): NetcodeVoicePeer[];
16
+ export declare function useNetcodeEvent<K extends NetcodeEventName>(event: K, handler: (payload: NetcodeEvents[K]) => void): void;
17
+ export declare function useNetcodeMessage<T = unknown>(type: string, handler: NetcodeMessageHandler<T>): void;
18
+ export type { NetVarOptions, NetcodeClient, NetcodeEventName, NetcodeEvents, NetcodeLobby, NetcodeMessage, NetcodeMessageHandler, NetcodeObjectChange, NetcodeObjectState, NetcodeOptions, NetcodePeer, NetcodePeerStats, NetcodeVoicePeer, };
package/dist/index.js ADDED
@@ -0,0 +1,119 @@
1
+ import { createContext, createElement, useCallback, useContext, useEffect, useMemo, useState, } from 'react';
2
+ import { createNetcode, } from 'openrtc-netcode';
3
+ const NetcodeContext = createContext(null);
4
+ export function NetcodeProvider({ client, options, autoStart = true, children }) {
5
+ const resolvedClient = useMemo(() => client ?? createNetcode(options), [client, options]);
6
+ useEffect(() => {
7
+ if (!autoStart) {
8
+ return undefined;
9
+ }
10
+ void resolvedClient.start();
11
+ return () => {
12
+ void resolvedClient.stop();
13
+ };
14
+ }, [autoStart, resolvedClient]);
15
+ return createElement(NetcodeContext.Provider, { value: resolvedClient }, children);
16
+ }
17
+ export function useNetcode() {
18
+ const client = useContext(NetcodeContext);
19
+ if (!client) {
20
+ throw new Error('useNetcode must be used within a NetcodeProvider.');
21
+ }
22
+ return client;
23
+ }
24
+ export function useLobby() {
25
+ const client = useNetcode();
26
+ const [lobby, setLobby] = useState(() => client.lobby);
27
+ useEffect(() => client.on('lobby', setLobby), [client]);
28
+ return lobby;
29
+ }
30
+ export function usePeers() {
31
+ const client = useNetcode();
32
+ const [peers, setPeers] = useState(() => client.peers);
33
+ useEffect(() => {
34
+ const refresh = () => setPeers(client.peers);
35
+ const stops = [
36
+ client.on('peer:joined', refresh),
37
+ client.on('peer:left', refresh),
38
+ client.on('peer:connected', refresh),
39
+ client.on('peer:disconnected', refresh),
40
+ client.on('member:metadata', refresh),
41
+ ];
42
+ refresh();
43
+ return () => {
44
+ for (const stop of stops) {
45
+ stop();
46
+ }
47
+ };
48
+ }, [client]);
49
+ return peers;
50
+ }
51
+ export function usePeerStats() {
52
+ const client = useNetcode();
53
+ const [stats, setStats] = useState(() => client.peerStats);
54
+ useEffect(() => {
55
+ const refresh = () => setStats(client.peerStats);
56
+ const stops = [
57
+ client.on('peer:stats', refresh),
58
+ client.on('peer:joined', refresh),
59
+ client.on('peer:left', refresh),
60
+ client.on('peer:connected', refresh),
61
+ client.on('peer:disconnected', refresh),
62
+ ];
63
+ refresh();
64
+ return () => {
65
+ for (const stop of stops) {
66
+ stop();
67
+ }
68
+ };
69
+ }, [client]);
70
+ return stats;
71
+ }
72
+ export function useNetVar(key, initial, options) {
73
+ const client = useNetcode();
74
+ const netVar = useMemo(() => client.state.define(key, initial, options), [client, key]);
75
+ const [value, setValue] = useState(() => netVar.value);
76
+ useEffect(() => netVar.onChange((next) => setValue(next)), [netVar]);
77
+ const setNetVar = useCallback((next) => netVar.set(next), [netVar]);
78
+ return [value, setNetVar];
79
+ }
80
+ export function useNetworkObjects(kind) {
81
+ const client = useNetcode();
82
+ const [objects, setObjects] = useState(() => filterObjects(client.objects.list(), kind));
83
+ useEffect(() => {
84
+ const refresh = () => setObjects(filterObjects(client.objects.list(), kind));
85
+ refresh();
86
+ return client.objects.onChange(refresh);
87
+ }, [client, kind]);
88
+ return objects;
89
+ }
90
+ export function useNetcodeVoicePeers() {
91
+ const client = useNetcode();
92
+ const [peers, setPeers] = useState(() => client.voice.listPeers());
93
+ useEffect(() => {
94
+ const refresh = () => setPeers(client.voice.listPeers());
95
+ const stops = [
96
+ client.voice.on('peers', setPeers),
97
+ client.voice.on('peer:stream', refresh),
98
+ client.voice.on('peer:left', refresh),
99
+ ];
100
+ refresh();
101
+ return () => {
102
+ for (const stop of stops) {
103
+ stop();
104
+ }
105
+ };
106
+ }, [client]);
107
+ return peers;
108
+ }
109
+ export function useNetcodeEvent(event, handler) {
110
+ const client = useNetcode();
111
+ useEffect(() => client.on(event, handler), [client, event, handler]);
112
+ }
113
+ export function useNetcodeMessage(type, handler) {
114
+ const client = useNetcode();
115
+ useEffect(() => client.onMessage(type, handler), [client, type, handler]);
116
+ }
117
+ function filterObjects(objects, kind) {
118
+ return kind ? objects.filter((object) => object.kind === kind) : objects;
119
+ }
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "openrtc-netcode-react",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "description": "React context and hooks for openrtc-netcode.",
8
+ "keywords": [
9
+ "openrtc",
10
+ "netcode",
11
+ "react",
12
+ "multiplayer"
13
+ ],
14
+ "author": "Bryant Hargreaves",
15
+ "license": "SEE LICENSE IN LICENSE",
16
+ "homepage": "https://openrtc.app",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/bluestarburst/openrtc.git",
20
+ "directory": "packages/openrtc-netcode-react"
21
+ },
22
+ "main": "dist/index.js",
23
+ "types": "dist/index.d.ts",
24
+ "files": [
25
+ "dist",
26
+ "LICENSE",
27
+ "README.md"
28
+ ],
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.js"
33
+ },
34
+ "./package.json": "./package.json"
35
+ },
36
+ "scripts": {
37
+ "build": "rm -rf dist && tsc",
38
+ "prepublishOnly": "pnpm run build",
39
+ "test": "vitest run tests",
40
+ "test:watch": "vitest"
41
+ },
42
+ "dependencies": {
43
+ "openrtc-netcode": "^0.1.0"
44
+ },
45
+ "peerDependencies": {
46
+ "react": "^18.2.0 || ^19.0.0"
47
+ },
48
+ "devDependencies": {
49
+ "@types/node": "^24.10.1",
50
+ "@types/react": "^19.2.7",
51
+ "@types/react-dom": "^19.2.3",
52
+ "happy-dom": "^20.5.0",
53
+ "react": "^19.2.3",
54
+ "react-dom": "^19.2.3",
55
+ "typescript": "^5.8.3",
56
+ "vitest": "^4.0.18"
57
+ }
58
+ }