All Projects → Rapsssito → React Native Tcp Socket

Rapsssito / React Native Tcp Socket

Licence: mit
React Native TCP socket API for Android, iOS & macOS with client SSL/TLS support

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to React Native Tcp Socket

Simplenet
An easy-to-use, event-driven, asynchronous network application framework compiled with Java 11.
Stars: ✭ 164 (+46.43%)
Mutual labels:  network, tcp, tcp-server, tcp-client
Netcoreserver
Ultra fast and low latency asynchronous socket server & client C# .NET Core library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
Stars: ✭ 799 (+613.39%)
Mutual labels:  tcp-server, tcp-client, ssl, tls
Cppserver
Ultra fast and low latency asynchronous socket server & client C++ library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
Stars: ✭ 528 (+371.43%)
Mutual labels:  tcp-server, tcp-client, ssl, tls
Simpletcp
Simple wrapper for TCP client and server in C# with SSL support
Stars: ✭ 99 (-11.61%)
Mutual labels:  tcp, tcp-server, tcp-client, ssl
Packetsender
Network utility for sending / receiving TCP, UDP, SSL
Stars: ✭ 1,349 (+1104.46%)
Mutual labels:  tcp, tcp-server, tcp-client, ssl
Easytcp
Simple framework for TCP clients and servers. Focused on performance and usability.
Stars: ✭ 60 (-46.43%)
Mutual labels:  tcp, tcp-server, tcp-client, ssl
SuperSimpleTcp
Simple wrapper for TCP client and server in C# with SSL support
Stars: ✭ 263 (+134.82%)
Mutual labels:  ssl, tcp, tcp-server, tcp-client
Tinytcpserver
A small tcp server working under Mono or .NET (4.0) and provides hooks for handling data exchange with clients (works under mono and .net). Behaviour/protocol/reaction could be specified via custom C# script.
Stars: ✭ 14 (-87.5%)
Mutual labels:  network, tcp, tcp-server, tcp-client
Hprose Nodejs
Hprose is a cross-language RPC. This project is Hprose 2.0 for Node.js
Stars: ✭ 297 (+165.18%)
Mutual labels:  tcp, tcp-server, tcp-client
Message Io
Event-driven message library for building network applications easy and fast.
Stars: ✭ 321 (+186.61%)
Mutual labels:  network, tcp, tcp-server
Hp Socket
High Performance TCP/UDP/HTTP Communication Component
Stars: ✭ 4,420 (+3846.43%)
Mutual labels:  network, tcp, ssl
Hisocket
It is a lightweight client socket solution, you can used it in C# project or Unity3d
Stars: ✭ 275 (+145.54%)
Mutual labels:  network, tcp, net
EasyFileTransfer
An easy way to transfer file with any size on network with tcp protocol.
Stars: ✭ 30 (-73.21%)
Mutual labels:  tcp, tcp-server, tcp-client
Tacopie
C++ TCP Library - NO LONGER MAINTAINED
Stars: ✭ 359 (+220.54%)
Mutual labels:  tcp, tcp-server, tcp-client
Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (-40.18%)
Mutual labels:  tcp, tcp-server, tcp-client
ctsTraffic
ctsTraffic is a highly scalable client/server networking tool giving detailed performance and reliability analytics
Stars: ✭ 125 (+11.61%)
Mutual labels:  tcp, tcp-server, tcp-client
Twisted
Event-driven networking engine written in Python.
Stars: ✭ 4,442 (+3866.07%)
Mutual labels:  network, ssl, tls
Bizsocket
异步socket,对一些业务场景做了支持
Stars: ✭ 469 (+318.75%)
Mutual labels:  tcp, tcp-server, tcp-client
Simpleunitytcp
🖧 Simple Unity Project to show how TCP communication are builded in C# without multi-threading or Unity network (Unet) involved.
Stars: ✭ 22 (-80.36%)
Mutual labels:  tcp, tcp-server, tcp-client
tcp-net
Build tcp applications in a stable and elegant way
Stars: ✭ 42 (-62.5%)
Mutual labels:  tcp, tcp-server, tcp-client

react-native-tcp-socket

React Native TCP socket API for Android, iOS & macOS with client SSL/TLS support. It allows you to create TCP client and server sockets, imitating Node's net API functionalities (check the available API for more information).

Table of Contents

Getting started

Install the library using either Yarn:

yarn add react-native-tcp-socket

or npm:

npm install --save react-native-tcp-socket

Overriding net

Since react-native-tcp-socket offers the same API as Node's net, in case you want to import this module as net or use require('net') in your JavaScript, you must add the following lines to your package.json file.

{
  "react-native": {
    "net": "react-native-tcp-socket"
  }
}

In addition, in order to obtain the TS types (or autocompletion) provided by this module, you must also add the following to your custom declarations file.

...
declare module 'net' {
    import TcpSockets from 'react-native-tcp-socket';
    export = TcpSockets;
}

If you want to avoid duplicated net types, make sure not to use the default node_modules/@types in your tsconfig.json "typeRoots" property.

Check the example app provided for a working example.

Using React Native >= 0.60

Linking the package manually is not required anymore with Autolinking.

  • iOS Platform:

    $ cd ios && pod install && cd .. # CocoaPods on iOS needs this extra step

  • Android Platform:

    Modify your android/build.gradle configuration to match minSdkVersion = 21:

    buildscript {
      ext {
        ...
        minSdkVersion = 21
        ...
      }
    

Self-Signed SSL (only available for React Native > 0.60)

You will need a metro.config.js file in order to use a self-signed SSL certificate. You should already have this file in your root project directory, but if you don't, create it. Inside a module.exports object, create a key called resolver with another object called assetExts. The value of assetExts should be an array of the resource file extensions you want to support.

If you want to support .pem files (plus all the already supported files), your metro.config.js would like like this:

const {getDefaultConfig} = require('metro-config');
const defaultConfig = getDefaultConfig.getDefaultValues(__dirname);

module.exports = {
  resolver: {
    assetExts: [...defaultConfig.resolver.assetExts, 'pem'],
  },
  // ...
};

Using React Native < 0.60

You then need to link the native parts of the library for the platforms you are using. The easiest way to link the library is using the CLI tool by running this command from the root of your project:

$ react-native link react-native-tcp-socket

If you can't or don't want to use the CLI tool, you can also manually link the library using the instructions below (click on the arrow to show them):

Manually link the library on iOS
  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-tcp-socket and add TcpSockets.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libTcpSockets.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<
Manually link the library on Android
  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import com.asterinet.react.tcpsocket.TcpSocketPackage; to the imports at the top of the file
  • Add new TcpSocketPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-tcp-socket'
    project(':react-native-tcp-socket').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-tcp-socket/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      implementation project(':react-native-tcp-socket')
    

React Native Compatibility

To use this library you need to ensure you are using the correct version of React Native. If you are using a version of React Native that is lower than 0.60 you will need to upgrade before attempting to use this library latest version.

react-native-tcp-socket version Required React Native Version
4.X.X, 3.X.X >= 0.60.0
1.4.0 >= Unknown

Usage

Import the library:

import TcpSocket from 'react-native-tcp-socket';
// const net = require('react-native-tcp-socket');

Client

// Create socket
const client = TcpSocket.createConnection(options, () => {
  // Write on the socket
  client.write('Hello server!');

  // Close socket
  client.destroy();
});

client.on('data', function(data) {
  console.log('message was received', data);
});

client.on('error', function(error) {
  console.log(error);
});

client.on('close', function(){
  console.log('Connection closed!');
});

Server

const server = TcpSocket.createServer(function(socket) {
  socket.on('data', (data) => {
    socket.write('Echo server ' + data);
  });

  socket.on('error', (error) => {
    console.log('An error ocurred with client socket ', error);
  });

  socket.on('close', (error) => {
    console.log('Closed connection with ', socket.address());
  });
}).listen({ port: 12345, host: '0.0.0.0' });

server.on('error', (error) => {
  console.log('An error ocurred with the server', error);
});

server.on('close', () => {
  console.log('Server closed connection');
});

SSL Client

const client = TcpSocket.createConnection({
    port: 8443,
    host: "example.com",
    tls: true,
    // tlsCheckValidity: false, // Disable validity checking
    // tlsCert: require('./selfmade.pem') // Self-signed certificate
});

// ...

Note: In order to use self-signed certificates make sure to update your metro.config.js configuration.

API

Here are listed all methods implemented in react-native-tcp-socket, their functionalities are equivalent to those provided by Node's net (more info on #41). However, the methods whose interface differs from Node are marked in bold.

TcpSocket

createConnection()

createConnection(options[, callback]) creates a TCP connection using the given options.

createConnection: options

Required. Available options for creating a socket. It must be an object with the following properties:

Property Type iOS/macOS Android Description
port <number> Required. Port the socket should connect to.
host <string> Host the socket should connect to. IP address in IPv4 format or 'localhost'. Default: 'localhost'.
timeout <number> If set, will be used to call setTimeout(timeout) after the socket is created, but before it starts the connection.
localAddress <string> Local address the socket should connect from. If not specified, the OS will decide. It is highly recommended to specify a localAddress to prevent overload errors and improve performance.
localPort <number> Local port the socket should connect from. If not specified, the OS will decide.
interface <string> Interface the socket should connect from. If not specified, it will use the current active connection. The options are: 'wifi', 'ethernet', 'cellular'.
reuseAddress <boolean> Enable/disable the reuseAddress socket option. Default: true.
tls <boolean> Enable/disable SSL/TLS socket creation. Default: false.
tlsCheckValidity <boolean> Enable/disable SSL/TLS certificate validity check. Default: true.
tlsCert <any> CA file (.pem format) to trust. If null, it will use the device's default SSL trusted list. Useful for self-signed certificates. See example for more info. Default: null.

Note: The platforms marked as ❌ use the default value.

Server

listen()

listen(options[, callback]) creates a TCP server socket using the given options.

listen: options

Required. Available options for creating a server socket. It must be an object with the following properties:

Property Type iOS/macOS Android Description
port <number> Required. Port the socket should listen to.
host <string> Host the socket should listen to. IP address in IPv4 format or 'localhost'. Default: '0.0.0.0'.
reuseAddress <boolean> Enable/disable the reuseAddress socket option. Default: true.

Note: The platforms marked as ❌ use the default value.

Maintainers

Acknowledgments

License

The library is released under the MIT license. For more information see LICENSE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].