ConnectyCube / connectycube-reactnative-samples

Chat and Video Chat code samples for React Native, ConnectyCube
https://connectycube.com
Apache License 2.0
124 stars 111 forks source link

[Help needed] How to add onMessage listener in function component #208

Closed aravi365 closed 3 years ago

aravi365 commented 3 years ago

Tried adding the code provided in the documentation, but there is nothing happening in the event of a new message arrived.

ConnectyCube.chat.onMessageListener = onMessage;

function onMessage(userId, message) {
    console.log('[ConnectyCube.chat.onMessageListener] callback:', userId, message)
}

Please guide the best practices to fetch the live incoming messages in functional component using listeners (receiver side)

I am getting ConnectyCube.chat.isConnected false every second time that makes the listener not alive. Also, the chat history is taking too much time to get it loaded

aravi365 commented 3 years ago

ConnectyCube.chat.onMessageListener = () => console.warn('MESSAGE RECEIVED');

i am in doubt, where to add this code inorder to catch the live messages in the chat. I am using function components instead of class.

DaveLomber commented 3 years ago

You can add it whether you want, but better - in some long-lived place, e.g. some service class, not in component itself

When other user calls ConnectyCube.chat.send(..), then you will get the message inside ConnectyCube.chat.onMessageListener