An Android messaging application that connects to the EchoWave WebSocket server for real-time chat communication.
Demo_Websocket is the Android client companion for the EchoWave server. It provides a native Android interface for sending and receiving real-time messages through WebSocket connections.
- Real-time Messaging: Instant message delivery via Socket.IO WebSocket
- Local Database: SQLite Room database for offline message storage
- Auto Device Registration: Automatic device ID generation and persistence
- Message History: Load and display chat history from server
- Pagination: Load older messages by scrolling to the top
- Modern UI: Material Design with RecyclerView
- MVVM Architecture: ViewModel and LiveData for reactive UI
- Automatic Reconnection: Handles network interruptions gracefully
βββββββββββββββββββββββββββ
β MainActivity β
β (UI Layer) β
βββββββββββββ¬ββββββββββββββ
β
βββββββββββββΌββββββββββββββ
β ChatViewModel β
β (ViewModel Layer) β
βββββββββββββ¬ββββββββββββββ
β
βββββββββββββΌββββββββββββββ
β Room Database β
β (Data Layer) β
βββββββββββββββββββββββββββ
β
βββββββββββββΌββββββββββββββ
β WebSocketManager β
β (Network Layer) β
βββββββββββββ¬ββββββββββββββ
β
βΌ
EchoWave Server
- Android Studio (Arctic Fox or higher)
- Min SDK: 21 (Android 5.0 Lollipop)
- Target SDK: 34
- Java: 8 or higher
- EchoWave Server: Must be running (Setup Guide)
-
Clone or open the project in Android Studio
cd C:\Users\user\AndroidStudioProjects\Demo_Websocket
-
Configure Server URL
Open
WebSocketManager.javaand update the server URL:private static final String SERVER_URL = "http://YOUR_SERVER_IP:3000";
- For localhost on physical device: Use your computer's IP address
- For Android Emulator: Use
http://10.0.2.2:3000 - For production: Use your server's domain (e.g.,
https://echowave.example.com)
-
Sync Gradle
- Click "Sync Now" in Android Studio when prompted
- Or go to
File > Sync Project with Gradle Files
-
Build and Run
- Connect an Android device or start an emulator
- Click the "Run" button (βΆ) or press
Shift + F10
- Socket.IO Android Client v2.1.0 - WebSocket communication
- Room Database v2.6.1 - Local SQLite storage
- Gson v2.10.1 - JSON parsing
- RecyclerView - Message list display
- ViewModel - UI state management
- LiveData - Reactive data observation
- Material Design - UI components
app/src/main/java/com/presentation/demo/
βββ MainActivity.java # Main activity
βββ adapter/
β βββ MessageAdapter.java # RecyclerView adapter
βββ database/
β βββ AppDatabase.java # Room database instance
β βββ Message.java # Message entity
β βββ MessageDao.java # Message data access
β βββ DeviceInfo.java # Device entity
β βββ DeviceInfoDao.java # Device data access
βββ viewmodel/
β βββ ChatViewModel.java # ViewModel for chat
βββ websocket/
βββ WebSocketManager.java # Socket.IO manager
app/src/main/res/
βββ layout/
β βββ activity_main.xml # Main layout
β βββ item_message_sent.xml # Sent message item
β βββ item_message_received.xml # Received message item
βββ drawable/
βββ bg_message_sent.xml # Sent message background
βββ bg_message_received.xml # Received message background
-
Start EchoWave Server
cd C:\Projects\Node\EchoWave npm start
-
Find Your Computer's IP
# Windows ipconfig # Look for IPv4 Address
-
Update SERVER_URL in
WebSocketManager.javaprivate static final String SERVER_URL = "http://192.168.1.XXX:3000";
private static final String SERVER_URL = "http://10.0.2.2:3000";
10.0.2.2is the special alias to your host machine's localhost from the Android emulator
register
socket.emit("register", deviceData, callback);send_message
socket.emit("send_message", messageData, callback);load_more_messages
socket.emit("load_more_messages", paginationData, callback);new_message - Receive new messages
socket.on("new_message", onNewMessage);user_connected - User joined notification
socket.on("user_connected", onUserConnected);user_disconnected - User left notification
socket.on("user_disconnected", onUserDisconnected);CREATE TABLE messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
serverId INTEGER,
senderDeviceId TEXT,
senderName TEXT,
receiverDeviceId TEXT,
message TEXT,
timestamp INTEGER,
isBroadcast INTEGER,
isSent INTEGER
);CREATE TABLE device_info (
id INTEGER PRIMARY KEY AUTOINCREMENT,
deviceId TEXT UNIQUE,
deviceName TEXT,
createdAt INTEGER
);On first launch, the app:
- Generates a unique device ID (via EchoWave server using Faker)
- Saves it to Room database
- Reuses the same ID on subsequent launches
- Initial load: 50 latest messages
- Scroll to top: Loads 50 older messages
- Prevents duplicates using server message ID
- Messages are stored in local SQLite database
- Chat history persists across app restarts
- Syncs with server on reconnection
- LiveData observes database changes
- UI updates automatically when new messages arrive
- No manual refresh needed
-
Single Device Test
- Open the app
- Send a message
- Verify it appears in the list
-
Multiple Devices Test
- Install app on 2+ devices
- Send messages from each device
- Verify all devices receive messages in real-time
-
Web + Android Test
- Open web UI:
http://localhost:3000/chat - Open Android app
- Send messages from both
- Verify cross-platform messaging works
- Open web UI:
// Check connection status
if (webSocketManager.isConnected()) {
Log.d("WebSocket", "Connected to server");
}Problem: App shows "Disconnected from server"
Solutions:
- β Verify EchoWave server is running
- β Check SERVER_URL in WebSocketManager.java
- β Ensure phone and computer are on the same network
- β Disable firewall or allow port 3000
- β
For emulator, use
10.0.2.2instead oflocalhost
Problem: Same message appears twice
Solution: Already fixed! The app now checks for existing messages before inserting.
Problem: Chat history doesn't load
Solutions:
- β Check Logcat for errors
- β
Verify database is created (
device_infoandmessagestables) - β Ensure server has messages in MySQL database
Problem: Gradle sync fails
Solutions:
- β Update Android Gradle Plugin
- β Sync project with Gradle files
- β
Invalidate caches:
File > Invalidate Caches / Restart - β Check internet connection for dependencies
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />- Device IDs are stored locally (not secure for production)
- No authentication implemented (add JWT tokens for production)
- Messages are sent in plain text (use HTTPS/WSS in production)
- No encryption on local database (use SQLCipher for sensitive data)
- User authentication with username/password
- Private messaging (one-to-one chat)
- Image/file sharing
- Push notifications
- Message read receipts
- Typing indicators
- User profiles with avatars
- Message search functionality
- Dark mode support
- EchoWave Server: ../../../Projects/Node/EchoWave/README.md
- Socket.IO Android: https://socket.io/docs/v4/client-api/
- Room Database: https://developer.android.com/training/data-storage/room
This project is licensed under the MIT License.
This is a demo project. For production use, please implement proper authentication, encryption, and security measures.
Built with β€οΈ for Android | Powered by EchoWave Server