Disconnect from web socket

302 views
Skip to first unread message

Evans Attafuah

unread,
May 23, 2015, 7:57:20 AM5/23/15
to fireba...@googlegroups.com
Hello,

Is it advisable to disconnect the user from the connection ( web socket = calling offline manually ) on activity pause or when app is in background?

Rob DiMarco

unread,
May 25, 2015, 4:11:32 PM5/25/15
to fireba...@googlegroups.com
Hi Evans -

Disconnecting from Firebase could be useful in cases where you only need to load some initial set of data, and aren't interested in subscribing to any changes to that data. Otherwise, of course, disconnecting the connection will disable the real-time capability of Firebase.

Speaking generally, it will depend on your specific use case and how frequently you intend on reading from and writing to Firebase - but we recommend keeping the connection active to take advantage of the speed of Firebase.

On Sat, May 23, 2015 at 4:57 AM, Evans Attafuah <evans.a...@gmail.com> wrote:
Hello,

Is it advisable to disconnect the user from the connection ( web socket = calling offline manually ) on activity pause or when app is in background?

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/88759324-934b-4cde-84c2-6c7aadaaead8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

GmailMe

unread,
May 25, 2015, 4:42:30 PM5/25/15
to fireba...@googlegroups.com
Hello Rob,

Thanks for this. I also thought it makes sense to keep the connection alive for better user experience. My colleagues were more concerned about the cost. 

Sent from my iPhone 6
You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/6mWKzT442N8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.

Evans Attafuah

unread,
Jun 28, 2015, 7:07:09 AM6/28/15
to fireba...@googlegroups.com
Hello Rob,

I noticed something very unusual in my app and wanted to found out. I am using presence api** from firebase so I know when a user is online or offline.

My question is if user A is chatting with user B and user B loses connection. How will user A know; in my case I use if user B is online don't send a push notification but am not able to determine if user B goes offline. 

when user B closes the app .info/connected event fires so it's easy to tell but when the user loses a connection this event doesn't fire. 

Can you help me? or suggest a better way to handle this sort of thing. 

Jacob Wenger

unread,
Jun 28, 2015, 3:45:51 PM6/28/15
to fireba...@googlegroups.com
Hi Evans,

This may not be clear from the presence docs guide, but the way you would do this is that you'd actually store everyone's presence status in Firebase itself. You could create a /presence/ node which has child keys which are people's uids. When a user arrives, you add them to the /presence/ node, indicating that they are currently online. So the node will look something like this:

{
  "presence": {
    "simplelogin:25151": true,
    "facebook:958163": true,
    ...
  }
}

When a user loses connection, you want to remove them from the /presence/ node. You can do this via a combination of .info/connected and onDisconnect(). The entire thing would look like this:

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.child(".info/connected").on("value", function(snapshot) {
  if (snapshot.val() === true) {
    // We're connected (or reconnected)! Do anything here that should happen only if online (or on reconnect).

    // Get a reference to the logged-in user's presence ref
    var userPresenceRef = ref.child("presence").child(uid);

    // Add the user to the presence ref
    userPresenceRef.set(true);

    // When the user is disconnected, remove them from the presence ref
    userPresenceRef.onDisconnect().remove();
  }
});

Now, any other user can just listen for the
uid of the person they are talking to and if that user is removed from the /presence/ node, they know that user went offline.

Hope that helps,
Jacob

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.

Evans Attafuah

unread,
Jun 28, 2015, 6:58:51 PM6/28/15
to fireba...@googlegroups.com
Hello Jacob,

Am doing the exact same thing and I write a timestamp for last seen. But during testing I put of the wifi from one of my test devices and nothing was written to firebase. 

meaning userPresenceRef.onDisconnect().remove(); never triggered. Let me do more tests and see exactly what the problem is. 

Michael Lehenbauer

unread,
Jun 28, 2015, 7:29:46 PM6/28/15
to fireba...@googlegroups.com
How long did you wait?  If a user disconnects cleanly (e.g. just closes the page), it'll typically trigger the onDisconnect actions immediately.  But sometimes if they don't disconnect cleanly, we won't detect it for a couple minutes so the onDisconnect actions will be a little bit delayed (this may happen with turning off your wifi depending on your device, OS, etc.).

-Michael

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.

Evans Attafuah

unread,
Jun 28, 2015, 7:49:56 PM6/28/15
to fireba...@googlegroups.com
Ok. then it took a while. Because I mark messages as read when users are "active in a two chat" and "online:connected to firebase" it affects my UX in a sense that when disconnect delays messages will be marked as read but in actual fact haven't been delivered. Maybe am doing this wrongly. 

But thanks for the info. Will check with my solution. and will keep testing.

thanks again.

--
You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/6mWKzT442N8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.

To post to this group, send email to fireba...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages