@void-snippets/react
useSocketEmit
Fire-and-forget emit and emitWithAck with compile-time event typing.
#What it does
Returns two functions for sending events to the server. Both are stable references and don't re-create on re-renders.
#emit(event, ...args): void
Sends an event without waiting for the server to respond. Throws synchronously if the socket is not connected.
typescript
const { emit } = useSocketEmit();
// TypeScript knows join-room takes one string argument
emit('join-room', roomId);
// TypeScript knows send-message takes { text, roomId }
emit('send-message', { text: 'Hello everyone!', roomId });
// ❌ TypeScript error — wrong argument shape
emit('send-message', 'just a string');#emitWithAck(event, ...args): Promise
Sends an event and waits for the server to acknowledge with a response. TypeScript gives a compile error if you call this on an event with no callback in its type signature.
typescript
const { emitWithAck } = useSocketEmit();
// update-profile declares a callback: (res: { status: 'ok' | 'error' }) => void
// emitWithAck strips the callback and returns Promise<{ status: 'ok' | 'error' }>
const result = await emitWithAck('update-profile', 'New Name');
if (result.status === 'ok') toast.success('Name updated!');
// ❌ TypeScript error — join-room has no callback in its type
await emitWithAck('join-room', roomId);