capacitor-tcp-socket-manager
is an Android-specific Capacitor plugin that enables seamless TCP socket communication for your hybrid mobile apps. It allows you to create a TCP server, manage client connections, send and receive messages, and handle various socket operations efficiently. Note: This plugin currently supports Android only.
- Start and stop a TCP server.
- Connect to a TCP server as a client.
- Send and receive messages.
- Manage multiple client connections.
- Get the local device's IP address.
- Limit maximum client connections (default is 10).
- Disconnect individual or all clients gracefully.
- Capacitor 4 or later
- Android Studio installed and configured
- Node.js and npm installed
- Install the plugin:
npm install @yesprasoon/capacitor-tcp-socket-manager
- Sync with Capacitor:
npx cap sync
- Rebuild the Android project:
npx cap open android
Starts a TCP server on the specified port.
Parameters:
port
: (optional, default: 8080) Port number to start the server on.
Returns:
ipAddress
: The IP address of the server.port
: The port on which the server is running.
Usage:
TcpSocketManager.startServer({ port: 8080 })
.then(response => console.log(`Server started on ${response.ipAddress}:${response.port}`))
.catch(error => console.error(error));
Stops the TCP server if running.
Usage:
TcpSocketManager.stopServer()
.then(() => console.log('Server stopped'))
.catch(error => console.error(error));
Gets the number of currently connected clients.
Returns:
count
: The number of connected clients.
Usage:
TcpSocketManager.getClientCount()
.then(result => console.log(`Connected clients: ${result.count}`))
.catch(error => console.error(error));
Connects to a TCP server.
Parameters:
ipAddress
: The IP address of the server.port
: (optional, default: 8080) Port number of the server.
Usage:
TcpSocketManager.connectToServer({ ipAddress: '192.168.0.100', port: 8080 })
.then(() => console.log('Connected to server'))
.catch(error => console.error(error));
Disconnects from the connected server.
Usage:
TcpSocketManager.disconnectFromServer()
.then(() => console.log('Disconnected from server'))
.catch(error => console.error(error));
Sends a message to the connected server.
Parameters:
message
: The message string to send.ipAddress
: (optional): The IP address of the server. If not provided, the stored IP address (from the connection step) will be used.port
: (optional, default: 8080): Port number of the server. If not provided, the stored port (from the connection step) will be used.
Usage:
TcpSocketManager.sendMessageToServer({ message: 'Hello Server!' })
.then(() => console.log('Message sent successfully'))
.catch(error => console.error('Failed to send message:', error));
Listens for incoming messages on the server.
Usage:
TcpSocketManager.addListener('receiveMessage', data => {
console.log(`Message received: ${data.message}`);
});
TcpSocketManager.startServer({ port: 8080 })
.then(response => console.log(`Server is running on ${response.ipAddress}:${response.port}`))
.catch(error => console.error(error));
TcpSocketManager.connectToServer({ ipAddress: '192.168.0.100', port: 8080 })
.then(() => {
return TcpSocketManager.sendMessageToServer({ message: 'Hello, Server!' });
})
.then(() => console.log('Message sent to server'))
.catch(error => console.error(error));
We are continuously improving this plugin. Future updates may include:
- TLS/SSL communication for secure connections.
- IP whitelisting for enhanced access control.
- Client authentication mechanisms.
- Message acknowledgment for reliable communication.
- Automatic client reconnection.
All suggestions are welcome!
- Ensure the port is not already in use by another application.
- Check for sufficient permissions in your app's AndroidManifest.xml.
- Verify the IP address and port are correct.
- Ensure the device is on the same network as the server.
This project is licensed under the MIT License. See the LICENSE file for more details.