How to open the XMPP connection in react native th

2019-08-24 04:28发布

问题:

I am using XMPP.js inside react native and using web socket to connect with the xmpp server. I can successfully connect with the chat server but I have to navigate to different pages inside the App so I have 2 options:

  1. I must open a new XMPP connection to the xmpp chat server everytime I navigate to a new page (using react Navigation for navigating) and close the old connection [I think its not recommended] or

  2. I must open the connection in the starting and use the same connection throughout the entire app. But how to achieve that?

Is there any alternative like services in react native with which I can open the XMPP connection in the backend and it must work throughout the entire app. I need the feasible solution for both Android+IOS

回答1:

You must open the connection in the starting and use the same connection throughout the entire app. You need to have some class like singleton or so which will hold an xmpp client, so you will use it across the entire app. Or you even can try to hold a global variable inside App.js with xmll client.

For example, here at ConnectyCube ReactNative SDK https://developers.connectycube.com/js/react-native there is a simple prototype object which holds an xmpp client. Then this object can be used in a flux container or as a global variable, something like here How to use global variables in React Native?