@karizma-studio/karizma-connection-cocos v1.6.1
Karizma Connection JS
A lightweight, TypeScript-based SDK that wraps KarizmaConnection functionality.
Table of Contents
Features
- Easy Setup: Seamlessly create connections to SignalR Hubs in just a few lines of code.
 - Automatic Reconnect: Built-in support for re-establishing dropped connections.
 - Two Communication Patterns:
- Send/Receive (fire-and-forget style)
 - Request/Response (send data and get a typed response back)
 
 - Node and Browser: Can be used in server-side applications (Node) or front-end frameworks (e.g., React, Vue, Angular).
 - TypeScript Friendly: Ships with full type definitions (
.d.ts). 
Installation
npm install @karizma-studio/karizma-connection-jsOr if you use Yarn:
yarn add @karizma-studio/karizma-connection-jsUsage
Connect To Server
import { Connection } from '@karizma-studio/karizma-connection-js';
const connection = new Connection();
await connection.connect('https://your-backend.com/hub');Sending Data
To send data (or invoke a method) on the server, use send(address: string, ...body: any[]): Promise<void>:
// Example of sending a message with a complex payload
await connection.send('SendMessageToGroup', {
  groupId: 'group-123',
  text: 'Hello group!',
  timestamp: new Date(),
});Request-Response Pattern
If your server method returns data, use request<TResponse>(address: string, ...body: any[]):
interface UserProfile {
  id: string;
  name: string;
  email: string;
}
const response = await connection.request<UserProfile>('GetUserProfile', { userId: '123' });
if (response.error) {
  console.error('Error code:', response.error.code);
  console.error('Error message:', response.error.message);
} else {
  console.log('Received user profile data:', response.result);
}API Reference
Connection Class
constructor()
Creates a new Connection instance.
connect(url: string): Promise<void>
Establishes a connection to the specified SignalR hub.
- url: The URL of your SignalR hub endpoint.
 
await connection.connect('https://your-signalr-endpoint/hub');disconnect(): Promise<void>
Gracefully closes the connection.
await connection.disconnect();on<T>(command: string, handler: EventHandler<T>): void
Registers an event handler for an incoming command.
- command: A string identifier for your server messages.
 - handler: A callback function to handle the incoming data.
 
connection.on('OnNewMessage', (data) => { console.log(data); });send(address: string, ...body: any[]): Promise<void>
Sends data to the server without expecting a response.
- address: The method name or command recognized by the server.
 - body: Any data you want to send (spreads into an array).
 
await connection.send('BroadcastMessage', { text: 'Hello World' });request<TResponse>(address: string, ...body: any[]): Promise<Response<TResponse>>
Sends data to the server expecting a typed response.
- address: The method name or command recognized by the server.
 - body: Any data you want to send (spreads into an array).
 - returns: A 
Response<TResponse>object containing.resultor.error. 
const response = await connection.request<UserProfile>('GetUserProfile', { userId: '123' });Types
Response<T>
The default interface for responses:
interface Response<T> {
    Result?: T;
    Error?: {
        Code: number;
        Message: string;
    };
}EventHandler<T>
Used as the type for event handlers:
type EventHandler<T> = (data: T) => void;Building and Testing
- Build the project:
npm run build - Run ESLint:
npm run lint - Format with Prettier:
npm run format - Run Tests (using Jest):
npm test 
License
Contributing
Contributions are welcome! If you find a bug or have a feature request, please open an issue or create a pull request.
- Fork the repository.
 - Create a new branch for your feature or bugfix.
 - Commit your changes.
 - Push the branch and open a Pull Request.
 
Thank you for your interest in improving this project!
Happy coding! If you have any questions or need further assistance, feel free to open an issue or a discussion on the GitHub repository.
8 months ago
8 months ago
8 months ago
8 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago