Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-tcp-socket

Package Overview
Dependencies
Maintainers
1
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-tcp-socket

React Native TCP socket API for Android & iOS

  • 3.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10K
decreased by-0.93%
Maintainers
1
Weekly downloads
 
Created
Source

react-native-tcp-socket

React Native TCP socket API for Android & iOS. It allows you to create TCP clients and servers sockets, simulating node's net API.

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
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
        ...
      }
    

    Modify your android/app/src/main/AndroidManifest.xml and add the following:

      <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    
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 TcpSocket.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libTcpSocket.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.reactlibrary.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:
      compile 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.61 you will need to upgrade before attempting to use this library latest version.

react-native-tcp-socket versionRequired React Native Version
1.3.x>= 0.61
1.2.2>= 0.??

Usage

Import the library:

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

Client

// Create socket
var client = TcpSocket.createConnection(options);

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!');
});

// Write on the socket
client.write("Hello server!");

// Close socket
client.destroy();

Server

var 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(12345, '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');
});

API

Client

createConnection()

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

options

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

PropertyTypeDescription
hostStringRequired. A valid server IP address in IPv4 format or "localhost".
portNumberRequired. A valid server port.
[localAddress]StringA valid local IP address to bind the socket. If not specified, the OS will decide. It is highly recommended to specify a localAddress to prevent overload errors and improve performance.
[localPort]NumberA valid local port to bind the socket. If not specified, the OS will decide.
[interface]StringThe interface to bind the socket. If not specified, it will use the current active connection. The options are: "wifi".

Server

listen()

listen(port[, host]) creates a TCP server socket listening on the given port. If the host is not explicity selected, the socket will be bound to '0.0.0.0'.

Maintainers

Looking for maintainers!

Contributing

PR's welcome!

Acknowledgments

License

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

Keywords

FAQs

Package last updated on 23 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc