firebase-chat icon indicating copy to clipboard operation
firebase-chat copied to clipboard

Why using EventBus when Realtime database is used

Open bandhiwal opened this issue 8 years ago • 4 comments

If we are using firebase realtime database it will automatically update the new message in onDataChanged() method if the chatActivity is open, then why the need of eventBus there to pass that a new message came

bandhiwal avatar Feb 28 '17 12:02 bandhiwal

EventBus is used to send a notification event from MyFirebaseMessagingService.java to ChatFragment.java. The main reason behind it is that Realtime database cannot function when the application is not running on user's device. So whenever a notification is received and clicked, the application automatically sends the user to the chat screen with the help of EventBus. A local broadcast receiver can also be used instead of EventBus, but EventBus is much more easier to implement, that's why I went with it.

crazyhitty avatar Feb 28 '17 12:02 crazyhitty

On click of notification pendingIntent would open the specific activity, then how eventBus is used here. i don't get it please tell me

bandhiwal avatar Feb 28 '17 12:02 bandhiwal

Oops sorry, I gave the wrong explanation without properly checking out my code. Yes, you are right about using Realtime database in this scenario.

As you can see in the code below, I only send the events when chat activity is opened.

// Don't show notification if chat activity is open.
if (!FirebaseChatMainApp.isChatActivityOpen()) {
    sendNotification(title,
            message,
            username,
            uid,
            fcmToken);
} else {
    EventBus.getDefault().post(new PushNotificationEvent(title,
            message,
            username,
            uid,
            fcmToken));
}

The main reasoning behind EventBus usage is that there was one case generating where I wasn't receiving messages, which I will explain using this simple example:

  • Let's consider 2 users A and B.
  • Suppose A and B both opens the chat screen at the same time for the first time.
  • Now if A sent a message to B, B won't be able to receive that message as it is not subscribed to any chat room as no chat rooms were actually created when both opened the chat screen. Chat rooms are created when any of them sends the first message.
  • So now, how does user B gets notified that a chat room has been created and starts receiving the messages?
  • Well, we know that each message sends a push notification to the receiver, so when the chat screen is opened, we can simply refresh the page and check for messages again.

But, this generates an issue, i.e., now everytime receiver will update the chat messages twice, which I think I overlooked before. So, thanks for asking this question. If you have any idea how I should fix this that would be extremely helpful. I would re-open this issue and try to fix it later :). Fixing this issue might generate some performance improvements too.

crazyhitty avatar Feb 28 '17 13:02 crazyhitty

I think that the getMessages method won't be called twice as I have put a check above it, still it is a wrong pattern.

crazyhitty avatar Feb 28 '17 13:02 crazyhitty