Message Types
Learn about the different message types supported by the useLopusChat hook
Overview
The useLopusChat
hook is a powerful tool for integrating Lopus AI’s generative UI capabilities into your React application. It manages Socket.io connections, message handling, and provides utilities for building AI-powered interfaces.
Message Types
The hook uses a type-safe message system to distinguish between sent and received messages. Understanding these types is crucial for effective message handling.
Received Messages (Server → Client)
These messages are sent from the server to the client and can be of the following types:
Sent Messages (Client → Server)
These messages are sent from the client to the server:
Type Guards
Type guards are essential for safely handling different message types. Use them to ensure type safety in your application.
Best Practices
- Type Safety: Always use type guards (
isReceivedMessage
,isSentMessage
) to handle different message types safely. - Error Handling: Wrap
sendQuery
andsendSubmission
calls in try-catch blocks to manage errors effectively. - Message Filtering: Use
getMessagesByType
with the appropriate message type enum for type-safe filtering. - Component Handling: Remember that component messages contain structured data that needs to be rendered.
- Cleanup: The hook handles cleanup automatically, but ensure to clean up any custom message handlers.
Examples
Handling Different Message Types
Error Handling
TypeScript Support
The hook provides full TypeScript support with discriminated unions for message types:
This structure should help you understand and utilize the useLopusChat
hook effectively, focusing on message types and best practices.