A real-time chat application built with Flutter using WebSocket connections, BLoC pattern, and Clean Architecture principles.
- Real-time Communication: Connect to any WebSocket server and exchange messages instantly
- Clean Architecture: Well-structured codebase following clean architecture principles
- State Management: Robust state management using BLoC pattern
- Connection Management: Easy connect/disconnect with visual connection status
- Error Handling: Comprehensive error handling and user feedback
- Modern UI: Material Design 3 with intuitive chat interface
- Message History: View all sent and received messages with timestamps
- Flexible Configuration: Connect to any WebSocket server URL
| Connection Screen | Chat Interface | Connection Status |
|---|---|---|
| Connect to WebSocket | Real-time messaging | Live status indicator |
This project follows Clean Architecture principles with clear separation of concerns:
lib/
โโโ ๐ฏ core/ # Core functionality
โ โโโ error/ # Error handling
โ โโโ network/ # Network layer
โ โโโ usecases/ # Base use case classes
โโโ ๐ฌ features/chat/ # Chat feature module
โ โโโ ๐๏ธ domain/ # Business logic layer
โ โ โโโ entities/ # Domain entities
โ โ โโโ repositories/ # Repository interfaces
โ โ โโโ usecases/ # Business use cases
โ โโโ ๐พ data/ # Data layer
โ โ โโโ models/ # Data models
โ โ โโโ repositories/ # Repository implementations
โ โโโ ๐จ presentation/ # UI layer
โ โโโ bloc/ # BLoC state management
โ โโโ pages/ # Screen widgets
โ โโโ widgets/ # Reusable UI components
โโโ ๐ง injection_container.dart # Dependency injection
โโโ ๐ main.dart # App entry point
- Domain Layer: Contains business logic, entities, and repository interfaces
- Data Layer: Implements repositories and handles data sources
- Presentation Layer: Contains UI components and state management
- Flutter - UI framework
- flutter_bloc - State management
- web_socket_channel - WebSocket communication
- get_it - Dependency injection
- equatable - Value equality
- dartz - Functional programming
-
Clone the repository
git clone https://github.com/sohailmahmud/websocket_chat_app.git cd websocket_chat_app -
Install dependencies
flutter pub get
-
Run the app
flutter run
- Launch the app - You'll see the connection interface
- Enter WebSocket URL - Default is
wss://echo.websocket.org(echo server for testing) - Tap "Connect" - Establish WebSocket connection
- Start chatting - Send messages using the input field
- Monitor status - Watch the connection indicator in the app bar
- Echo Server:
wss://echo.websocket.org- Echoes back your messages - Demo Server:
wss://ws.postman-echo.com/raw- Postman's WebSocket echo - Custom Server: Connect to your own WebSocket server
The app supports any WebSocket server that accepts text messages. Simply enter the WebSocket URL and connect.
- WebSocketClient: Manages low-level WebSocket connections
- ChatRepository: Abstracts WebSocket operations and data flow
- Use Cases: Encapsulate business operations (Connect, Disconnect, SendMessage)
- ChatBloc: Handles state management and user interactions
The app uses BLoC pattern with the following states:
ChatInitial- Initial stateChatConnecting- Connecting to WebSocketChatConnected- Successfully connected with message listChatDisconnected- Disconnected from WebSocketChatError- Error state with error message
ConnectToWebSocket- Initiate connectionDisconnectFromWebSocket- Close connectionSendChatMessage- Send a messageMessageReceived- Handle incoming message
# Run all tests
flutter test
# Run tests with coverage
flutter test --coverage
# View coverage report
genhtml coverage/lcov.info -o coverage/html
open coverage/html/index.html- Unit Tests: Domain layer logic and use cases
- Widget Tests: UI components and interactions
- Integration Tests: End-to-end WebSocket functionality
- Flutter SDK (latest stable version)
- Dart SDK
- Android Studio / VS Code
- iOS Simulator / Android Emulator
-
Install Flutter: Follow the official Flutter installation guide
-
Clone and setup:
git clone <repository-url> cd flutter-websocket-chat flutter pub get
-
Run in debug mode:
flutter run --debug
If you modify the BLoC files, you may need to regenerate code:
flutter packages pub run build_runner buildContributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Follow the existing code style and architecture
- Add tests for new features
- Update documentation as needed
- Ensure all tests pass before submitting PR
dependencies:
flutter:
sdk: flutter
flutter_bloc: ^8.1.3 # State management
equatable: ^2.0.5 # Value equality
dartz: ^0.10.1 # Functional programming
web_socket_channel: ^2.4.0 # WebSocket support
get_it: ^7.6.4 # Dependency injection
injectable: ^2.3.2 # DI annotationsdev_dependencies:
flutter_test:
sdk: flutter
injectable_generator: ^2.4.1 # Code generation
build_runner: ^2.4.7 # Build system
flutter_lints: ^2.0.0 # Linting rules- None at the moment. Please report any issues you encounter!
- Message Persistence - Save chat history locally
- Multiple Rooms - Support for different chat rooms
- File Sharing - Send and receive files
- User Authentication - Login/logout functionality
- Push Notifications - Background message notifications
- Message Encryption - End-to-end encryption
- Voice Messages - Audio message support
- Dark Mode - Theme customization
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2024 Sohail Mahmud
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Your Name
- GitHub: @sohailmahmud
- LinkedIn: LinkedIn
- Email: sohailmahmuud@gmail.com
- Flutter Team for the amazing framework
- BLoC Library for excellent state management
- WebSocket.org for WebSocket standards and testing tools
- The Flutter community for continuous support and contributions
โญ If you found this project helpful, please give it a star! โญ
If you have any questions or need help with the project:
- Check the Issues: Look through existing GitHub Issues
- Create a New Issue: If you can't find your answer, create a new issue
- Discussions: Use GitHub Discussions for general questions
Made with โค๏ธ using Flutter