/** * Remove the value at this location when the client disconnects * * @param listener A listener that will be triggered once the server has queued up the operation */ public void removeValue(CompletionListener listener) { setValue(null, listener); }
public static Completable set (final DatabaseReference ref, final Object value, final boolean onDisconnectRemoveValue) { return Completable.create(e -> { ref.setValue(value, (databaseError, databaseReference) -> { if(databaseError != null) { e.onError(new Throwable(databaseError.getMessage())); } else { e.onComplete(); } }); if(onDisconnectRemoveValue) { ref.onDisconnect().removeValue(); } }).subscribeOn(Schedulers.single()); } }
/** * Cancel any disconnect operations that are queued up at this location * * @param listener A listener that will be triggered once the server has cancelled the operations */ public void cancel(final CompletionListener listener) { cancelInternal(listener); }
private void onDisconnect() { // [START rtdb_on_disconnect_set] DatabaseReference presenceRef = FirebaseDatabase.getInstance().getReference("disconnectmessage"); // Write a string when this client loses connection presenceRef.onDisconnect().setValue("I disconnected!"); // [END rtdb_on_disconnect_set] // [START rtdb_on_disconnect_remove] presenceRef.onDisconnect().removeValue(new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError error, @NonNull DatabaseReference reference) { if (error != null) { Log.d(TAG, "could not establish onDisconnect event:" + error.getMessage()); } } }); // [END rtdb_on_disconnect_remove] // [START rtdb_on_disconnect_cancel] OnDisconnect onDisconnectRef = presenceRef.onDisconnect(); onDisconnectRef.setValue("I disconnected"); // ... // some time later when we change our minds // ... onDisconnectRef.cancel(); // [END rtdb_on_disconnect_cancel] }
@Override public void onDataChange(DataSnapshot snapshot) { boolean connected = snapshot.getValue(Boolean.class); if (connected) { DatabaseReference con = myConnectionsRef.push(); // When this device disconnects, remove it con.onDisconnect().removeValue(); // When I disconnect, update the last time I was seen online lastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP); // Add this device to my connections list // this value could contain info about the device or a timestamp too con.setValue(Boolean.TRUE); } }
/** * Ensure the data at this location is set to the specified value when the client is disconnected * (due to closing the browser, navigating to a new page, or network issues). <br> * <br> * This method is especially useful for implementing "presence" systems, where a value should be * changed or cleared when a user disconnects so that they appear "offline" to other users. * * @param value The value to be set when a disconnect occurs * @param listener A listener that will be triggered once the server has queued up the operation */ public void setValue(Object value, CompletionListener listener) { onDisconnectSetInternal(value, PriorityUtilities.NullPriority(), listener); }
/** * Remove the value at this location when the client disconnects * * @return The ApiFuture for this operation. */ public ApiFuture<Void> removeValueAsync() { return setValueAsync(null); }
/** * Provides access to disconnect operations at this location * * @return An object for managing disconnect operations at this location */ public OnDisconnect onDisconnect() { Validation.validateWritablePath(getPath()); return new OnDisconnect(repo, getPath()); }
/** * Ensure the data at this location is set to the specified value when the client is disconnected * (due to closing the browser, navigating to a new page, or network issues). <br> * <br> * This method is especially useful for implementing "presence" systems, where a value should be * changed or cleared when a user disconnects so that they appear "offline" to other users. * * @param value The value to be set when a disconnect occurs * @return The ApiFuture for this operation. */ public ApiFuture<Void> setValueAsync(Object value) { return onDisconnectSetInternal(value, PriorityUtilities.NullPriority(), null); }
@Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot != null) { mUserDatabase.child("online").onDisconnect().setValue(ServerValue.TIMESTAMP); } }
threadUsersRef.onDisconnect().removeValue();
/** * Ensure the data at this location is set to the specified value and priority when the client is * disconnected (due to closing the browser, navigating to a new page, or network issues). <br> * <br> * This method is especially useful for implementing "presence" systems, where a value should be * changed or cleared when a user disconnects so that they appear "offline" to other users. * * @param value The value to be set when a disconnect occurs * @param priority The priority to be set when a disconnect occurs * @param listener A listener that will be triggered once the server has queued up the operation */ public void setValue(Object value, Map priority, CompletionListener listener) { onDisconnectSetInternal(value, PriorityUtilities.parsePriority(path, priority), listener); }
/** * Cancel any disconnect operations that are queued up at this location * * @return The ApiFuture for this operation. */ public ApiFuture<Void> cancelAsync() { return cancelInternal(null); }
private void disconnectionTimestamp() { // [START rtdb_on_disconnect_timestamp] DatabaseReference userLastOnlineRef = FirebaseDatabase.getInstance().getReference("users/joe/lastOnline"); userLastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP); // [END rtdb_on_disconnect_timestamp] }
/** * Ensure the data at this location is set to the specified value and priority when the client is * disconnected (due to closing the browser, navigating to a new page, or network issues). <br> * <br> * This method is especially useful for implementing "presence" systems, where a value should be * changed or cleared when a user disconnects so that they appear "offline" to other users. * * @param value The value to be set when a disconnect occurs * @param priority The priority to be set when a disconnect occurs * @return The ApiFuture for this operation. */ public ApiFuture<Void> setValueAsync(Object value, double priority) { return onDisconnectSetInternal(value, PriorityUtilities.parsePriority(path, priority), null); }
@Override public void onDataChange(final DataSnapshot dataSnapshot) { Log.d(TAG, "DataSnapshot:" + dataSnapshot); if (dataSnapshot.getValue(Boolean.class)) { electricityLog.setTimestampOn(ServerValue.TIMESTAMP); final DatabaseReference currentLogDbRef = firebaseDatabase.child(FIREBASE_LOGS).child(key); currentLogDbRef.setValue(electricityLog); currentUserRef.setValue(true); currentUserRef.onDisconnect().setValue(false); electricityLog.setTimestampOff(ServerValue.TIMESTAMP); currentLogDbRef.onDisconnect().setValue(electricityLog); } }
/** * Ensure the data at this location is set to the specified value and priority when the client is * disconnected (due to closing the browser, navigating to a new page, or network issues). <br> * <br> * This method is especially useful for implementing "presence" systems, where a value should be * changed or cleared when a user disconnects so that they appear "offline" to other users. * * @param value The value to be set when a disconnect occurs * @param priority The priority to be set when a disconnect occurs * @param listener A listener that will be triggered once the server has queued up the operation */ public void setValue(Object value, String priority, CompletionListener listener) { onDisconnectSetInternal(value, PriorityUtilities.parsePriority(path, priority), listener); }
/** * Ensure the data at this location is set to the specified value and priority when the client is * disconnected (due to closing the browser, navigating to a new page, or network issues). <br> * <br> * This method is especially useful for implementing "presence" systems, where a value should be * changed or cleared when a user disconnects so that they appear "offline" to other users. * * @param value The value to be set when a disconnect occurs * @param priority The priority to be set when a disconnect occurs * @param listener A listener that will be triggered once the server has queued up the operation */ public void setValue(Object value, double priority, CompletionListener listener) { onDisconnectSetInternal(value, PriorityUtilities.parsePriority(path, priority), listener); }
/** * Ensure the data at this location is set to the specified value and priority when the client is * disconnected (due to closing the browser, navigating to a new page, or network issues). <br> * <br> * This method is especially useful for implementing "presence" systems, where a value should be * changed or cleared when a user disconnects so that they appear "offline" to other users. * * @param value The value to be set when a disconnect occurs * @param priority The priority to be set when a disconnect occurs * @return The ApiFuture for this operation. */ public ApiFuture<Void> setValueAsync(Object value, String priority) { return onDisconnectSetInternal(value, PriorityUtilities.parsePriority(path, priority), null); }