feat: add socket event contract for Tauri integration

- Documented the socket event contract for communication between the backend and frontend in Tauri mode.
- Introduced constants for `REQUEST_ENCRYPTION_KEY` and `ENCRYPTION_KEY_EVENT` to facilitate encryption key management.
- Updated `setupTauriSocketListeners` to handle the encryption key request and response flow.
This commit is contained in:
M3gA-Mind
2026-02-06 22:23:55 +05:30
parent c4837892c4
commit 2cf0bd2622
2 changed files with 31 additions and 2 deletions
+11
View File
@@ -170,6 +170,17 @@ const socket = io(BACKEND_URL, {
});
```
### Socket event contract (Tauri)
In Tauri mode, the Rust socket forwards server events to the frontend via the `server:event` Tauri event. The frontend listens and can emit back via `emitViaRustSocket` (see `utils/tauriSocket.ts`).
| Direction | Event name | Payload | Description |
|------------|----------------------------|----------------------------------|-------------|
| Backend → | `request:encryption_key` | (any) | Backend requests the clients encryption key. |
| Frontend → | `encryption_key` | `{ encryptionKey: string \| null }` | Frontend sends `encryptionKeyByUser` for the current user from authSlice. |
Constants: `REQUEST_ENCRYPTION_KEY`, `ENCRYPTION_KEY_EVENT` (exported from `utils/tauriSocket.ts`).
## MTProto Service (`services/mtprotoService.ts`)
Telegram MTProto client singleton.
+20 -2
View File
@@ -12,6 +12,10 @@
* Legacy bridge (for backwards compatibility during migration):
* - socket:should_connect / socket:should_disconnect
* - report_socket_connected / disconnected / error
*
* Socket event contract (backend <-> frontend):
* - Backend emits: REQUEST_ENCRYPTION_KEY — frontend should reply with encryption key.
* - Frontend emits: ENCRYPTION_KEY_EVENT — payload { encryptionKey: string | null } from authSlice.
*/
import { isTauri as coreIsTauri, invoke } from '@tauri-apps/api/core';
import { listen, UnlistenFn } from '@tauri-apps/api/event';
@@ -20,6 +24,12 @@ import { store } from '../store';
import { setSocketIdForUser, setStatusForUser } from '../store/socketSlice';
import { BACKEND_URL } from './config';
/** Event name the backend emits to request the clients encryption key. */
export const REQUEST_ENCRYPTION_KEY = 'request:encryption_key';
/** Event name the frontend emits with encryptionKeyByUser (authSlice) for the current user. */
export const ENCRYPTION_KEY_EVENT = 'encryption_key';
// Check if we're running in Tauri
export const isTauri = (): boolean => {
return coreIsTauri();
@@ -154,8 +164,16 @@ export async function setupTauriSocketListeners(): Promise<void> {
// Listen for forwarded server events
unlistenServerEvent = await listen<{ event: string; data: unknown }>('server:event', event => {
console.log('[TauriSocket] Server event:', event.payload.event, event.payload.data);
// Future: dispatch to specific handlers based on event type
const { event: eventName, data } = event.payload;
console.log('[TauriSocket] Server event:', eventName, data);
if (eventName === REQUEST_ENCRYPTION_KEY) {
const state = store.getState();
const userId =
state.user.user?._id ?? getSocketUserId();
const encryptionKey = state.auth.encryptionKeyByUser[userId] ?? null;
void emitViaRustSocket(ENCRYPTION_KEY_EVENT, { encryptionKey });
}
});
// Legacy: Listen for connect requests from Rust (backwards compat)