npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rn-tcp-socket

v3.4.3

Published

React Native TCP socket API for Android & iOS

Downloads

15

Readme

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):

  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)<
  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:
      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 version | Required React Native Version | | ----------------------------------------- | --------------------------------------------------------------------------------- | | 3.x.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.

createConnection: options

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

| Property | Type | iOS | 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'. | | 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. |

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

write()

  • data: <string> | <Buffer> | <Uint8Array>
  • encoding: <string>. Only used when data is string. Default: utf8.
  • callback : <Function>

write(data[, encoding][, callback]) sends data on the socket. The second parameter specifies the encoding in the case of a string — it defaults to UTF8 encoding.

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

Looking for maintainers!

Acknowledgments

License

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