https://github.com/yesprasoon/capacitor-tcp-socket-manager
https://github.com/yesprasoon/capacitor-tcp-socket-manager
android capacitor hybrid-apps ionic networking plugin socket tcp
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/yesprasoon/capacitor-tcp-socket-manager
- Owner: yesprasoon
- License: mit
- Created: 2025-01-05T14:17:56.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-10-10T06:21:16.000Z (8 months ago)
- Last Synced: 2025-10-17T13:25:16.836Z (8 months ago)
- Topics: android, capacitor, hybrid-apps, ionic, networking, plugin, socket, tcp
- Language: Java
- Homepage:
- Size: 133 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Capacitor TCP Socket Manager
`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.**
---



## Table of Contents
- [Features](#features)
- [Installation](#installation)
- [API Methods](#api-methods)
- [Server-Side Methods](#server-side-methods)
- [startServer](#startserver)
- [stopServer](#stopserver)
- [getClientCount](#getclientcount)
- [Client-Side Methods](#client-side-methods)
- [connectToServer](#connecttoserver)
- [disconnectFromServer](#disconnectfromserver)
- [sendMessageToServer](#sendmessagetoserver)
- [Events](#events)
- [receiveMessage](#receivemessage)
- [Example Usage](#example-usage)
- [Starting a Server](#starting-a-server)
- [Sending a Message from Client to Server](#sending-a-message-from-client-to-server)
- [Planned Features](#planned-features)
- [Troubleshooting](#troubleshooting)
- [License](#license)
---
## Features
- 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.
---
## Installation
### Prerequisites
- Capacitor 4 or later
- Android Studio installed and configured
- Node.js and npm installed
### Steps
1. Install the plugin:
For Capacitor 7:
```bash
npm install @yesprasoon/capacitor-tcp-socket-manager
```
For Capacitor 6:
```bash
npm install @yesprasoon/capacitor-tcp-socket-manager@^1.1.2
```
2. Sync with Capacitor:
```bash
npx cap sync
```
3. Rebuild the Android project:
```bash
npx cap open android
```
---
## API Methods
### Server-Side Methods
#### `startServer`
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:**
```javascript
TcpSocketManager.startServer({ port: 8080 })
.then(response => console.log(`Server started on ${response.ipAddress}:${response.port}`))
.catch(error => console.error(error));
```
#### `stopServer`
Stops the TCP server if running.
**Usage:**
```javascript
TcpSocketManager.stopServer()
.then(() => console.log('Server stopped'))
.catch(error => console.error(error));
```
#### `getClientCount`
Gets the number of currently connected clients.
**Returns:**
- `count`: The number of connected clients.
**Usage:**
```javascript
TcpSocketManager.getClientCount()
.then(result => console.log(`Connected clients: ${result.count}`))
.catch(error => console.error(error));
```
### Client-Side Methods
#### `connectToServer`
Connects to a TCP server.
**Parameters:**
- `ipAddress`: The IP address of the server.
- `port`: (optional, default: 8080) Port number of the server.
**Usage:**
```javascript
TcpSocketManager.connectToServer({ ipAddress: '192.168.0.100', port: 8080 })
.then(() => console.log('Connected to server'))
.catch(error => console.error(error));
```
#### `disconnectFromServer`
Disconnects from the connected server.
**Usage:**
```javascript
TcpSocketManager.disconnectFromServer()
.then(() => console.log('Disconnected from server'))
.catch(error => console.error(error));
```
#### `sendMessageToServer`
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:**
```javascript
TcpSocketManager.sendMessageToServer({ message: 'Hello Server!' })
.then(() => console.log('Message sent successfully'))
.catch(error => console.error('Failed to send message:', error));
```
---
## Events
### `receiveMessage`
Listens for incoming messages on the server.
**Usage:**
```javascript
TcpSocketManager.addListener('receiveMessage', data => {
console.log(`Message received: ${data.message}`);
});
```
---
## Example Usage
### Starting a Server
```javascript
TcpSocketManager.startServer({ port: 8080 })
.then(response => console.log(`Server is running on ${response.ipAddress}:${response.port}`))
.catch(error => console.error(error));
```
### Sending a Message from Client to Server
```javascript
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));
```
---
## Planned Features
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!
---
## Troubleshooting
### Problem: Server does not start
- Ensure the port is not already in use by another application.
- Check for sufficient permissions in your app's AndroidManifest.xml.
### Problem: Unable to connect to the server
- Verify the IP address and port are correct.
- Ensure the device is on the same network as the server.
---
## License
This project is licensed under the MIT License. See the LICENSE file for more details.