1.0.2 • Published 2 years ago

react-strophe-hook v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

What is React-Strophe-hook

Strophe.js is a popular library to handle xmpp requests on browser. When the library is published jquery was the hot thing. So , it follows jquery approaches. This hook is a wrapper on top of strophe.js to make react implementation easier.

a working implementation can be found here

installation

npm i react-strophe-hook

or

yarn add react-strophe-hook

Basic Implementation

import React, { useEffect } from 'react';
import { Strophe } from 'strophe.js';
import { useStrophe } from 'react-strophe-hook';

const BOSH_SERVER = 'wss://boshServer.com';
const connection = new Strophe.Connection(BOSH_SERVER);
const credentials = {
  jabid: id@jabberDomain.com,
  pass: password,
};

function App() {
  const {
    connect,
    connected,
    disconnect,
    connecting,
    disconnecting,
    disconnected,
    domainName,
    resource,
    bareJid,
  } = useStrophe({
    credentials,
    connection,
    showLogs: true,
  });

  useEffect(() => {
    connect();
    return () => {
      disconnect('unmounted');
    };
  }, []);


  return (
    <div>
      {connecting && <p>Connecting...</p>}
      {disconnecting && <p>Disconnecting...</p>}
      <div>
        <p>The server is {connected ? 'connected' : 'disconnected'}</p>
      </div>

      <div>
        {connected && (
          <button type="button" onClick={() => disconnect('TESTING')}>
            Disconnect
          </button>
        )}
        {disconnected && (
          <button type="button" onClick={connect}>
            Connect
          </button>
        )}
      </div>
    </div>
  );
}

export default App;

Parameters

nametyperequireddefaultdescription
credentials{jabid: string; pass: string;}trueundefinedcreddentials of jabb user and password
onConnect(reason?: string) => voidfalseundefinedExecutes when xmpp connection is established.
onConnecting(reason?: string) => voidfalseundefinedExecutes when xmpp connection is being established
onDisconnect(reason?: string) => voidfalseundefinedExecutes when xmpp connection is Disconnected
onDisconnecting(reason?: string) => voidfalseundefinedExecutes when xmpp connection is being Disconnected
onConnFail(reason?: string) => voidfalseundefinedExecutes when failed to establish xmpp connection
connectionStrophe.ConnectiontruenullStrophe connection object
onMessage(msg: Element) => booleanfalsenullCatches message stanzas, if the function returns false, it'll catch single message and the handler will be deleted, if true is returned it will catch all catch call message stanzas.If the function is not passed and showLogs is set to true, it'll console log the messages.
onPresence(presence: Element) => booleanfalsenullCatches presence stanzas, if the function returns false, it'll catch single presence and the handler will be deleted, if true is returned it will catch all catch call presence stanzas. If the function is not passed and showLogs is set to true, it'll console log the presences.
onIq(iq: Element) => booleanfalsenullCatches iq stanzas, if the function returns false, it'll catch single iq and the handler will be deleted, if true is returned it will catch all catch iq stanzas.If the function is not passed and showLogs is set to true, it'll console log the iqs.
handlers{ handlerFunc: (_payload: Element) => boolean; matcher:{ namespace?: string; name: 'message' | 'iq' | 'presence'; type?: string; id?: string; from?: string; }; }[]false[]takes array of objects. Each object has two key,value pair. handlerFunc same as onMessage, onIq, onStanza functions. matcher an object has five keys namespace?: string; name: 'message''iq''presence'; type?: string; id?: string; from?: string; all the keys are optional. If an matcher is passed as an empty object, it'll match all the stanzas.
showLogsbooleanfalsefalseif set to true, it'll show all event logs

Return values

nametypedescription
connect() => voidcall this function to establish connection
disconnect() => voidcall this function to gracefully disconnect
connectedbooleanreturns true if connection established successfully
connectingbooleanreturns true if connection is being established, can be used to show a loader
disconnectedbooleanreturns true is connection disconnected
disconnectingbooleanreturns true if connection is being disconnected, can be used to show a loader
connFailbooleanreturns true if connection is failed
domainNamestring|undefinedreturns domain name once connection is established
bareJidstringreturns jid without domain name
resourcestringreturns resource
1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago