Socket
Socket
Sign inDemoInstall

react-native-find-local-devices

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-find-local-devices

It can be helpful when you try to get a list of your local devices over WiFi with websocket connection.


Version published
Maintainers
1
Weekly downloads
17
decreased by-22.73%

Weekly downloads

Readme

Source

🚀 react-native-find-local-devices

npm version build platform license

example

It can be helpful when you try to get a list of your local devices over WiFi when the devices includes at least one websocket connection.

This package allows you detect all devices over your local network with websocket connection.

You've to add a timeout and an array of ports as parameters. The package will try to create a connection with those ports and return the ip adresses which have successful connection.

See the example: https://github.com/RichardRNStudio/react-native-find-local-devices/tree/main/example

NOTICE: It doesn't work with IOS yet. If you can help me in this case please contact me on the following email: info@rnstudio.hu

This package has been written for the PC Controller react-native application as a submodule.

Visit the PC Controller website

Installation

npm install react-native-find-local-devices --save

Running the example project

NOTICE: It requires a real device with Wi-Fi connection

npm run example:android

Usage

import FindLocalDevices from 'react-native-find-local-devices';
import { DeviceEventEmitter } from 'react-native';

// Don't forget to call DeviceEventEmitter.removeAllListeners() when discovering isn't running.
// WARNING: DeviceEventEmitter.removeAllListeners will remove all of your listeners. 
// See the example folder. There is an advanced example how you can create 
// and remove listeners independent of any other listeners.
// MAIN BEHAVIOUR: 
// ...
  DeviceEventEmitter.addListener('NEW_DEVICE_FOUND', (device) => {
    console.log(`NEW DEVICE FOUND: ${device.ipAddress}:${device.port}`);
    // This listener will be activated at the moment when the device has been found.
    // FORMAT: {ipAddress: "192.168.1.66", port: 70}
  });

  DeviceEventEmitter.addListener('RESULTS', (devices) => {
    // ALL OF RESULTS when discovering has been finished.
    // FORMAT: [{ipAddress: "192.168.1.66", port: 70}, {ipAddress: "192.168.1.69", port: 85}]
  });

  DeviceEventEmitter.addListener('CHECK', (device) => {
    // This listener will be activated in that moment when package checking a device.
    // FORMAT: {ipAddress: "192.168.1.65", port: 70}
  });

  DeviceEventEmitter.addListener('NO_DEVICES', () => {
    // This listener will be activated at the end of discovering.
  });

  DeviceEventEmitter.addListener('NO_PORTS', () => {
    // This listener will be activated if you don't pass any ports to the package.
  });

  DeviceEventEmitter.addListener('CONNECTION_ERROR', (error) => {
    // Handle error messages for each socket connection
    // console.log(error.message);
  });

  // Getting local devices which have active socket server on the following ports:
  FindLocalDevices.getLocalDevices({
    ports: [70, 85, 1200],
    timeout: 40
  });

  // When the discovering is running, you can cancel that with the following function:
  FindLocalDevices.cancelDiscovering();
// ...

Create a listener and remove it

  // ...
  const newDeviceFoundSubscription = DeviceEventEmitter.addListener(
    NEW_DEVICE_FOUND,
    (device) => {
      if (device.ipAddress && device.port) {
        console.log(device);
      }
    }
  );
  // ...
  if(newDeviceFoundSubscription) newDeviceFoundSubscription.remove();
  // ...

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT

Keywords

FAQs

Last updated on 05 Feb 2021

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc