react-native-smart-socket
v1.0.0
Published
Advanced WebSocket package with file processing, real-time presence, and chat features for React Native
Maintainers
Readme
React Native Smart Socket
Disclaimer: This package is currently in a pre-release stage and is not yet ready for production use. Some of the core functionalities, such as the
ConnectionManagerandFileProcessor, are mock implementations and need to be replaced with actual logic.
React Native Smart Socket is an advanced WebSocket package for React Native, designed to provide a robust and feature-rich real-time communication solution. It comes with built-in support for file processing, real-time presence, chat features, and much more.
Features
- WebSocket Management: Automatically manages WebSocket connections, with auto-reconnect and offline support.
- Real-time Presence: Track user presence and status in real-time.
- Typing Indicators: Real-time typing indicators for chat applications.
- Room Management: Easily manage rooms and participants.
- File Uploads: Built-in support for file uploads with chunking, compression, and progress tracking.
- Voice Messages: Send and receive voice messages with ease.
- Custom Hooks: A set of custom hooks to easily integrate the socket functionalities into your React Native components.
- TypeScript Support: Written in TypeScript for a better developer experience.
Installation
npm install react-native-smart-socketor
yarn add react-native-smart-socketUsage
Here is a simple example of how to use the useSmartSocket hook to connect to a WebSocket server.
import React from 'react';
import { View, Text } from 'react-native';
import { useSmartSocket } from 'react-native-smart-socket';
const App = () => {
const config = {
url: 'ws://your-socket-server.com',
userId: 'user-123',
};
const { socket, isConnected, connectionError } = useSmartSocket(config);
return (
<View>
<Text>Connection Status: {isConnected ? 'Connected' : 'Disconnected'}</Text>
{connectionError && <Text>Error: {connectionError.message}</Text>}
</View>
);
};
export default App;API Reference
Hooks
The package provides a set of custom hooks to interact with the WebSocket server.
useSmartSocket(config): The main hook to initialize and manage the WebSocket connection.useChat(roomId, socket): A comprehensive hook for building a complete chat experience.useChatMessages(roomId, socket): A hook to manage chat messages in a room.useUserPresence(socket): A hook to track online users.useTypingIndicator(roomId, socket): A hook to manage typing indicators in a room.useRooms(socket): A hook to manage rooms.useFileUpload(socket): A hook for handling file uploads.useFilePicker(): A hook for picking files from the device.useVoiceRecorder(): A hook for recording voice messages.useFileMessages(roomId, socket): A hook to get all file messages in a room.useFileChat(socket): A hook that combines file-related hooks for a streamlined file-based chat experience.useApiWatcher(config, socket): A hook to watch for API updates.
SmartSocketCore Class
The core class that manages the WebSocket connection and all the related functionalities. The useSmartSocket hook returns an instance of this class.
Configuration
The useSmartSocket hook takes a configuration object with the following properties:
| Property | Type | Description |
| ---------------- | ----------------- | --------------------------------------------------------------------------- |
| url | string | The URL of the WebSocket server. |
| userId | string | The ID of the current user. |
| username | string | The name of the current user. |
| autoConnect | boolean | Whether to automatically connect to the server. Defaults to true. |
| reconnect | ReconnectConfig | Configuration for the auto-reconnect feature. |
| presence | PresenceConfig | Configuration for the user presence feature. |
| typing | TypingConfig | Configuration for the typing indicator feature. |
| rooms | string[] | An array of room IDs to automatically join upon connection. |
| apiConfig | ApiConfig | Configuration for the API watcher feature. |
| fileProcessing | FileProcessingConfig | Configuration for the file processing feature. |
Events
The SmartSocketCore instance emits a number of events that you can listen to using the on method.
connected: Fired when the socket is connected.disconnected: Fired when the socket is disconnected.reconnecting: Fired when the socket is trying to reconnect.error: Fired when an error occurs.message: Fired when a new message is received.room_joined: Fired when a user joins a room.room_left: Fired when a user leaves a room.user_joined: Fired when a new user comes online.user_left: Fired when a user goes offline.presence_update: Fired when a user's presence is updated.user_typing: Fired when a user starts or stops typing.file_upload_progress: Fired to report the progress of a file upload.file_upload_complete: Fired when a file upload is complete.file_upload_error: Fired when a file upload fails.
Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue.
License
This project is licensed under the MIT License.
