@turbo-ing/turbo-p2p
v0.7.11
Published
A library for blazingly fast peer-to-peer communication.
Readme
@turbo-ing/turbo-p2p
A library for blazingly fast peer-to-peer communication.
Installation
npm install @turbo-ing/turbo-p2pImporting
// Import classes
import { RTC, Batcher, RTCServerConfig } from "@turbo-ing/turbo-p2p";
// Import types
import {
Connection,
GroupConfig,
LobbyType,
// ... other types as needed
} from "@turbo-ing/turbo-p2p";Basic Usage
Setting up an RTC connection
// Create an RTC instance
const rtc = new RTC((messages) => {
// Handle received messages
console.log('Received messages:', messages);
});
// Initialize the connection
await rtc.init();
// Create an offer with configuration
const config = {
general: {
public: true,
gamespace: "myApp",
type: LobbyType.complete,
capacity: 10
},
stream: {
/* Stream configuration */
},
channel: {
/* Channel configuration */
}
};
await rtc.createOffer(config);Server Configuration
You can configure the RTC server URLs in several ways:
1. Using Environment Variables
In a Node.js environment, create a .env file:
RTC_SERVER_HTTP_URL=http://your-server:4001
RTC_SERVER_WS_URL=ws://your-server:40022. Using Next.js Public Environment Variables
In a Next.js app, use the NEXT_PUBLIC_ prefix in your .env file:
NEXT_PUBLIC_RTC_SERVER_HTTP_URL=http://your-server:4001
NEXT_PUBLIC_RTC_SERVER_WS_URL=ws://your-server:40023. Passing Server Configuration Directly
You can pass server URLs directly to the RTC constructor:
const serverConfig: RTCServerConfig = {
httpUrl: 'http://your-server:4001',
wsUrl: 'ws://your-server:4002'
};
const rtc = new RTC(
(messages) => {
console.log('Received messages:', messages); //primitive message handler
},
serverConfig
);Using the Batcher
// Create a batcher with a single handler
const simpleBatcher = new Batcher((messages) => {
console.log('Processing batch of messages:', messages);
});
// Or create a batcher with channel-specific handlers
const channelBatcher = new Batcher({
'chat': (messages) => console.log('Chat messages:', messages),
'events': (messages) => console.log('Event messages:', messages)
});
// Add messages to buffer
simpleBatcher.addToBuffer(['message1', 'message2']);
// Add messages to specific channels
channelBatcher.addToBuffer([
{ channel: 'chat', message: 'Hello!' },
{ channel: 'events', message: 'UserJoined' }
]);Documentation
For more detailed documentation, please refer to the type definitions provided in the package.
License
MIT
