简体   繁体   中英

way to recieve message in out of the chat component in react native

I need to have a real time message listener in react native I have chat component when user login to chat component message's work correctly, but when user A connect to chat but user B in home screen message don't receive by user B because websocket receive message only load in chat component. so I want to have a global receive function for listen new message please help me

In my opinion, You have two ways to do it.

1- Have your WebSocket linked to a component at the base of your app, with a listener on the new_user_event and pass add it to your store with an action. In the same way, you did in the chat component. your server when the

2- Is to check on the server-side if a user is connected or the chat screen is mounted (or the user is on the chat screen). If the user is not in the chat screen send a silent notification to app with the info of the new user


[Edit]

You need some kind of store to save data and share it between your screens. I could be redux or context or directly on the local storage of your app.

__

Another way which is not clean is to handle all the event in the base of the app and pass the data as props to the children

if you want it to work all around the app import your socket at the "App.js", and create a function to store the messages in localstorage on receive parameter, i have tested this method it should work fine;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM