How to open the XMPP connection in react native throughout the entire app (using xmpp.js)?

老子叫甜甜 提交于 2019-12-24 19:41:34

问题


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?



来源:https://stackoverflow.com/questions/53114492/how-to-open-the-xmpp-connection-in-react-native-throughout-the-entire-app-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!