You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

react-native-websocket-messaging

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-websocket-messaging

Wrapper for react native websocket communication

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

React native websocket messaging ( client IPC )

Wrapper for react native websocket communication

Installing

npm install react-native-websocket-messaging --save
- or -
yarn add react-native-websocket-messaging

Usage

import React from 'react';
import WSMessaging from 'react-native-websocket-messaging';

export default class Example extends React.Component
{
    constructor()
    {
        this.connection = new Connection( this, [ { host: 'wss://127.0.0.1', port: 21 } ] );
    }

    componentDidMount()
    {
        this.connection.send( 'hello',
        {
            data : {}
        },
        ( status, data ) =>
        {
            if( status === 'NETWORKING' )
            {
                switch( data )
                {
                    case 'SENT' 	: console.log( 'message:sent' ); break;
                    case 'RECEIVED' : console.log( 'message:received' ); break;
                    case 'REPLIED' 	: console.log( 'message:replied' ); break;
                    case 'TIMEOUT' 	: console.log( 'message:timeout' ); break;
                }
            }
        }, 10000 )
        .then( async ( message ) =>
        {
            //YOUR LOGIC HERE

            return message.reply({ ok: true }, 5000);
        })
        .then( async ( message ) =>
        {
            //YOUR LOGIC HERE, SECOND MESSAGE REPLY
        })
        .catch(( e ) =>
        {
            //Error handling here ( timeot, ... )
        });
    }

    async on_pair_message( message )
    {
        //HANDLING YOR MESSAGE FROM SERVER
    }
}

API

changeServers

Easily changing ip & port Example:

import WSMessaging from 'react-native-websocket-messaging';

export default class Example extends React.Component
{
    componentWillUnmount()
	{
		this.connection.changeServers( [ { host: 'wss://127.0.0.1', port: 21 } ] );
	}
}

connected

Detection of connect status Example:

import WSMessaging from 'react-native-websocket-messaging';

export default class Example extends React.Component
{
    componentWillUnmount()
	{
		this.connection.connected;
	}
}

destroy

Destroy connection Example:

import WSMessaging from 'react-native-websocket-messaging';

export default class Example extends React.Component
{
    componentWillUnmount()
	{
		this.connection.destroy();
	}
}

Keywords

ws

FAQs

Package last updated on 16 Jul 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