|
| 1 | +# @mixcore/signalr |
| 2 | + |
| 3 | +SignalR real-time communication services for Mixcore SDK. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Real-time Communication**: Full SignalR client implementation with connection management |
| 8 | +- **Type Safety**: Complete TypeScript support with strongly typed interfaces |
| 9 | +- **Chat Services**: High-level chat service abstraction for common messaging patterns |
| 10 | +- **Connection Management**: Automatic reconnection, state management, and error handling |
| 11 | +- **Security**: Token-based authentication with configurable access token factory |
| 12 | +- **Framework Agnostic**: Can be used in any JavaScript/TypeScript environment |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install @mixcore/signalr |
| 18 | +# or |
| 19 | +yarn add @mixcore/signalr |
| 20 | +# or |
| 21 | +pnpm add @mixcore/signalr |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +### Basic SignalR Service |
| 27 | + |
| 28 | +```typescript |
| 29 | +import { SignalRService } from '@mixcore/signalr'; |
| 30 | + |
| 31 | +const service = new SignalRService({ |
| 32 | + hubUrl: 'https://your-hub-url/hub', |
| 33 | + accessTokenFactory: () => localStorage.getItem('access_token'), |
| 34 | + automaticReconnect: true, |
| 35 | + onConnected: () => console.log('Connected'), |
| 36 | + onDisconnected: (error) => console.log('Disconnected', error) |
| 37 | +}); |
| 38 | + |
| 39 | +// Start connection |
| 40 | +await service.start(); |
| 41 | + |
| 42 | +// Listen for messages |
| 43 | +service.onMessage('receive_message', (message) => { |
| 44 | + console.log('Received:', message); |
| 45 | +}); |
| 46 | + |
| 47 | +// Send messages |
| 48 | +await service.invoke('SendMessage', 'Hello World'); |
| 49 | +``` |
| 50 | + |
| 51 | +### Chat Service |
| 52 | + |
| 53 | +```typescript |
| 54 | +import { ChatService } from '@mixcore/signalr'; |
| 55 | + |
| 56 | +const chatService = new ChatService({ |
| 57 | + baseUrl: 'https://your-api-url', |
| 58 | + hubPath: '/hub/chat', |
| 59 | + accessTokenFactory: () => localStorage.getItem('access_token') |
| 60 | +}); |
| 61 | + |
| 62 | +// Start chat service |
| 63 | +await chatService.start(); |
| 64 | + |
| 65 | +// Send messages |
| 66 | +await chatService.sendMessage('Hello, AI!'); |
| 67 | + |
| 68 | +// Listen for responses |
| 69 | +chatService.onMessageReceived((message) => { |
| 70 | + if (message.data.response) { |
| 71 | + console.log('AI Response:', message.data.response); |
| 72 | + } |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +## API Reference |
| 77 | + |
| 78 | +### SignalRService |
| 79 | + |
| 80 | +Main service class for SignalR connections. |
| 81 | + |
| 82 | +#### Constructor Options |
| 83 | + |
| 84 | +- `hubUrl`: SignalR hub URL |
| 85 | +- `accessTokenFactory`: Function that returns the access token |
| 86 | +- `logLevel`: SignalR logging level (optional) |
| 87 | +- `automaticReconnect`: Enable automatic reconnection (default: true) |
| 88 | +- `onConnected`: Connection established callback |
| 89 | +- `onDisconnected`: Connection lost callback |
| 90 | +- `onReconnecting`: Reconnection started callback |
| 91 | +- `onReconnected`: Reconnection completed callback |
| 92 | +- `onError`: Error callback |
| 93 | + |
| 94 | +#### Methods |
| 95 | + |
| 96 | +- `start()`: Start the SignalR connection |
| 97 | +- `stop()`: Stop the SignalR connection |
| 98 | +- `invoke(method, ...args)`: Invoke a server method and wait for response |
| 99 | +- `send(method, ...args)`: Send a message to server without waiting for response |
| 100 | +- `onMessage(event, handler)`: Register message handler |
| 101 | +- `offMessage(event, handler)`: Unregister message handler |
| 102 | +- `isConnected()`: Check if connection is active |
| 103 | +- `getConnectionState()`: Get current connection state |
| 104 | +- `dispose()`: Clean up resources |
| 105 | + |
| 106 | +### ChatService |
| 107 | + |
| 108 | +High-level service for chat functionality. |
| 109 | + |
| 110 | +#### Constructor Options |
| 111 | + |
| 112 | +- `baseUrl`: Base URL for the SignalR hub (optional, defaults to current origin) |
| 113 | +- `hubPath`: Hub path (optional, defaults to '/hub/llm_chat') |
| 114 | +- All SignalRService options except `hubUrl` |
| 115 | + |
| 116 | +#### Methods |
| 117 | + |
| 118 | +- `sendMessage(content)`: Send a chat message |
| 119 | +- `sendChatMessage(message)`: Send a structured chat message |
| 120 | +- `onMessageReceived(handler)`: Listen for incoming messages |
| 121 | +- `offMessageReceived(handler)`: Stop listening for messages |
| 122 | +- All SignalRService methods |
| 123 | + |
| 124 | +## Security Considerations |
| 125 | + |
| 126 | +- Always use HTTPS in production |
| 127 | +- Implement proper access token validation |
| 128 | +- Sanitize all incoming messages |
| 129 | +- Use environment variables for configuration |
| 130 | +- Implement rate limiting on the server side |
| 131 | + |
| 132 | +## Framework Integration |
| 133 | + |
| 134 | +This package is framework-agnostic and can be used with: |
| 135 | + |
| 136 | +- Vanilla JavaScript/TypeScript |
| 137 | +- React |
| 138 | +- Vue |
| 139 | +- Angular |
| 140 | +- Svelte/SvelteKit |
| 141 | +- Node.js |
| 142 | + |
| 143 | +## License |
| 144 | + |
| 145 | +See LICENSE file in the repository root. |
0 commit comments