Skip to content
This repository has been archived by the owner on Sep 29, 2018. It is now read-only.

Why using EventBus when Realtime database is used #2

Open
bandhiwal opened this issue Feb 28, 2017 · 4 comments
Open

Why using EventBus when Realtime database is used #2

bandhiwal opened this issue Feb 28, 2017 · 4 comments

Comments

@bandhiwal
Copy link

bandhiwal commented Feb 28, 2017

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 bandhiwal changed the title Why using Why using EventBus when Realtime database is used Feb 28, 2017
@crazyhitty
Copy link
Owner

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 crazyhitty self-assigned this Feb 28, 2017
@bandhiwal
Copy link
Author

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

@crazyhitty
Copy link
Owner

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
Copy link
Owner

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants