Any disadvantages to calling keepSynced on same ref multiple times? - firebase

Due to the flow of my app I'm forced to call keepSynced(true) on the same ref every time the user opens the app. I was wondering if it's bad to do so or if Firebase just ignores any redundant keepSynced() calls on the same ref.
How about calling keepSynced(true) on a sub-ref of a ref you already called keepSynced(true) on, are those ignored too?
I'm really looking for a conclusive answer.

keeySynced is either on or off for a path given by a reference. There is no "multiple keepSynced" state - that would be pointless to implement inside the SDK since there is no advantage to doing so.

You only need to call keepSynced(true) once. The way I implement it is to extend the Application Class.
public class GlobalApp extends Application {
#Override
public void onCreate() {
super.onCreate();
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
FirebaseDatabase.getInstance().getReference().keepSynced(true);
}
}

Calling keepSynced(true) on a node ensures that the Firebase Database client will synchronize that node whenever it has a connection to the database servers. There is no built-in API to keep a node synchronized when there is no such connection.

keepSynced(true);
will be useful if we enable offline support
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
If we set keepSynced(true), then whenever a user's internet connection is online, it will update it's node data. More explanation can be read in here
For example : if other user delete the node, than if another user offline. The offline user data will still exist if we're not setting the keepSynced(true).
In some case it will make a force close.
So My conclusion is, either we didn't support offline database,
or support offline but with keepSynced(true). There is also another option, we can choose whenever to keepSynced true or false.

Related

Azure Notification Hub device registration

Since AppCenter retiring at the end of this year, I have started migrating to Azure-Notification-Hub.
But the documentation for notification-hub is not clear at all. Especially the documentation for Xamarin.Android. It does not match with their latest SDK.
In the latest (version 1.1.1) azure-notification-hub SDK for Android (or Xamarin.Android) there's no need to implement FirebaseMessagingService. NotificationHub.Start() method registers the device in the Notification-Hub. A device registered with this way gets notifications without any problem.
NotificationHub.Start(Application, <HubName>, <ConnectionString>);
Addition tags to existing NotificationHub instance are also straightforward with the new SDK.
NotificationHub.AddTag("username:user123");
But in Registration Management official doc states that devices can register with the notification-hub either from client-side or from server-side. Is it necessary to use one of those methods if my app registered with the notification-hub using the NotificationHub.Start() method? Or do I missing something?
Also, when I was using the AppCenter, I have used the AppCenter-InstallId to target a specific device.
With that in mind is it possible to use the NotificationHub.InstallationId to use as a tag (eg: "handle:<devce's InstallationId>") to send device-specific notifications?
Is it necessary to use one of those methods if my app registered with the notification-hub using the NotificationHub.Start() method?
When you invoke NotificationHub.start(Application, ...), the Android SDK will listen for changes like added tags, new FirebaseMessagingService tokens, etc. Anytime it detects a change, it will invoke an InstallationAdapter to inform a backend of the new details.
The default InstallationAdapter will send an PUT request to the Azure Notification Hubs backend as documented here. This is what is created by NotificationHub.start(Application, string, string); for people who are not hosting their own backend, this is a sensible default.
If you have your own backend where you track devices, or you're just looking to keep your credentials server-side, you can swap out the InstallationAdapter to be a class that invokes your API. All you need to do is implement the InstallationAdapter interface and initialize the SDK by calling NotificationHub.start(Application, InstallationAdapter).
If you use the NotificationHub.start(...) methods as indicated above, there is no further registration action required.
With that in mind is it possible to use the NotificationHub.InstallationId to use as a tag (eg: "handle:<devce's InstallationId>") to send device-specific notifications?
Yes! This documentation walks you through how to use the special tag format $InstallationId:{YOUR_TAG_ID} to target a specific device.
When you've used NotificationHub.start(), if you do not specify an InstallationId, it will generate one for you.
Question about setting the InstallationId and/or UserId. If I'm using Microsoft.Azure.NotificationHubs.Client, which makes more sense to do.
Should I set the InstallationId via the $InstallationId Tag (see here: https://learn.microsoft.com/en-us/azure/notification-hubs/notification-hubs-push-notification-registration-management#installations) or via implementing the InstallationEnrichmentAdapter and set it via a call to installation.InstallationId = in the EnrichInstallation method?
Additionally, the Microsoft.Azure.NotificationHubs.Client.Installation class provides a UserId property that can be updated too.
I'm also moving my push notifications from AppCenter to Azure Notification Hub and want to reuse my existing AppCenter InstallId.
i am able to add tags and userid as below(java)
NotificationHub.start(this.getApplication(), BuildConfig.hubName, BuildConfig.hubListenConnectionString);
NotificationHub.setInstallationId("123");
Set<String> tags = new HashSet<>();
tags.add("role_memeber");
NotificationHub.setUserId("123");
NotificationHub.addTags(tags);
sdk: com.microsoft.azure:notification-hubs-android-sdk:1.1.6

Calling .setPersistenceEnabled(false) when logging out of app, not working

In my Flutter/Dart mobile app I make use of Firebase RTDB persistence to enable offline use of the app.
My understanding is that to enable persistence you have to make the call, as per the following piece of code, before using any database references to eg. query the database. I use the following piece of code to enable persistence immediately after loading the app and it works fine:
FirebaseDatabase firebaseDatabase = FirebaseDatabase.instance;
bool _success = await firebaseDatabase.setPersistenceEnabled(true);
print(_success); // Prints true, so persistence is set 'on'.
When I logout of the app I attempt to turn persistence off with:
bool _success = await firebaseDatabase.setPersistenceEnabled(false);
print(_success); // Prints false, so persistence is still 'on', ie. the call failed.
I assume the reason persistence cannot be turned off is because there have been calls to db references prior to trying to switch it off.
This leads to three questions, I guess:
Should I be worried about turning it off at all, when I logout? The reason I attempt it is good house-keeping, mainly. I clean up shared preferences, close keepsyncd's, etc when logout is run. Also, though, the user can have multiple userids to login and I want to make sure that I am not retaining persisted data from their previous login id.
Related to 1, does setting persistence to false clear the cache of
data and potential queued calls to the db?
If the answers to 1 and 2 are 'yes', how can I switch persistence off given the code I'm using to do so keeps telling me it failed?
The typical way to handle this is to enable persistence once a user logs in.
Once disk persistence has been enabled and your app has used the database, it cannot be turned off. The documentation says this about it:
The returned Future will complete with true if the operation was successful or false if the persistence could not be set (because database references have already been created).
That last bit is clearly the case for you: you've been using the database already, which means that disk persistence is on.
To your specific questions:
Unfortunately the data in the local cache cannot be cleared up through the API at the moment. It is a valid feature request, but for now you'll have to assume that any data on the device can be seen by any user on that device (or device profile).
Disabling disk persistence keep the client from adding data to the cache. It does not clear any existing data in the cache.

Where to place domain services in AxonIQ

I have a user aggregate which is created using CreateUser command which consists of aggregate identifier and username.
Along with that i have domain service that communicates with mongo db and checks if username exists, if not it puts it there.
eg registerUsername(username) -> true / false whether it registered it or not
My question is, would it be good idea to create command handler on top of the user aggregate that would handle the CreateUser command and whether it has username or not will dispatch proper commands/events? like so:
#Component
class UserCommandHandler(
#Autowired
private val repository: Repository<User>,
#Autowired
private val eventBus: EventBus,
#Autowired
private val service: UniqueUserService
) {
#CommandHandler
fun createUser(cmd: CreateUser) {
if (this.service.registerUsername(cmd.username)) {
this.repository.newInstance { User(cmd.id) }
.handle(GenericCommandMessage(cmd))
} else {
this.eventBus.publishEvent(UserCreateFailed(cmd.id, cmd.username))
}
}
}
This question is not necessarily related to the set uniqueness in ddd but more of a question where should i put dependency of domain services? I could probably create user registration saga and inject that service inside saga but i think saga should only rely on command dispatching and not have any if/else logic.
I think the place to put your domain service depends on the use case at hand.
I typically try to have domain service do virtual no outbound calls to other services or databases, at all.
The domain service you're now conceiving however does exactly that to, like you're point out, solve the uniqueness issue.
In this situation, you could likely come by with the suggested approach.
You could also think of introducing a MessageHandlerInterceptor (or even fancier, a HandlerEnhancerDefinition as described here), specifically triggering on the create command and performing the desired check.
If it would be domain service like I depicted mine just now (e.g. zero outbound calls from domain service), then you can safely wire it in your command handling functions to perform some action.
If you're in a Spring environment, simply having your domain service as a bean and providing it as a parameter to your message handling function is sufficient for Axon to resolve it for you (through the means of ParameterResolvers, as described here).
Hope this helps you out #PolishCivil!

Guidelines for robust synchronisation of mobile client (iOS, Swift) with Realm Object Server

I have used the techniques in the RealmTask tutorial (https://realm.io/docs/tutorials/realmtasks/ ) to get a demonstration of synchronisation with the Realm Object Server working. However, as mentioned in realm mobile platform, how to connect while offline? , it is difficult to find design guidelines on realising a robust app in the presence of intermittent network connectivity. For example, the network might not be available when the app is first run, and in the tutorial example I think the login attempt would just time out after say 30 seconds.
From various sources, I have tried to outline an implementation approach on the client and have come up with the following:
=============================================================
At start-up of app
Create login credentials with
SyncCredentials.usernamePassword()
Check whether user credentials already exist using
SyncUser.all
If so, get the correct user using the appropriate key (UserId)
If a user is obtained, get the Realm configuration using
realmConfiguration = Realm.Configuration(SyncConfiguration(user, realmURL))
Attempt a log-in with
SyncUser.logIn with SyncCredentials
On completion, put the following on the main DispatchQueue (async)
realmConfiguration = Realm.Configuration(SyncConfiguration(user, realmURL))
if not logged in, repeat login attempts every N minutes until successful? E.g. to handle the situation when the network is unavailable when the app is started, but then becomes available?
Launch the rest of the app, making realmConfiguration available.
However, only access the Realm if realmConfiguration has been set up. Design the app so that it handles the scenario of realmConfiguration not being set up.
=============================================================
Is the above approach sensible, or is there a better solution?
Katsumi from Realm here. Our RealmTasks demo application may help you.
https://github.com/realm-demos/realm-tasks/tree/master/RealmTasks%20Apple
First, check whether the user has logged in or not at launched the app.
if configureDefaultRealm() {
window?.rootViewController = ContainerViewController()
window?.makeKeyAndVisible()
} else {
window?.rootViewController = UIViewController()
window?.makeKeyAndVisible()
logIn(animated: false)
}
https://github.com/realm-demos/realm-tasks/blob/master/RealmTasks%20Apple/RealmTasks%20iOS/AppDelegate.swift#L35
If the user has been logged in before, you can use user object that was cached before. (SyncUser.current or SyncUser.all)
If there is no cached user object (The user is the first time to use the app, or the user re-installs the app), show login view to signing up/in.
The former case (Use the cached user object) doesn't require network access, so you don't need to care about the offline situation.
The latter case (The user should signing up/in) requires network access, in that case, the best practice depends on the specification of the app. It is enough to show a just alert view that indicates requiring network for some apps, or use standalone Realm and then migrate synced realm after the app will be online.

Possibility for only currently connected (not authenticated) and admin user to read and write on certain location

Is there any way to write a security rule or is there any other approach that would make possible only for currently connected (not authenticated) user to write/read certain location - admin should also be able to write/read?
Can a rule be written that disallows users to read of complete list of entries and let them read only entry that matches some identifier that was passed from client?
I'm trying to exchange some data between user and Node.js application through Firebase and that data shouldn't be able to read or write by anyone else other than user and/or admin.
I know that one solution would be that user requests auth token on my server and uses it to authenticate on Firebase and that would make it possible to write rule which prevents reads and writes. However, I'm trying to avoid user connecting to my server so this solution is not first option.
This is in a way session based scenario which is not available in Firebase but I have
some ideas that could solve this kind of problem - if implemented before session management:
maybe letting admin write into /.info/ location which is observed by client for every change and can be read only by active connection - if I understood correctly how .info works
maybe creating .temp location for that purpose
maybe letting admin and connected client could have more access to connection information which would contain some connection unique id, that can be used to create location with that name and use it inside rule to prevent reading and listing to other users
Thanks
This seems like a classic XY problem (i.e. trying to solve the attempted solution instead of the actual problem).
If I understand your constraints correctly, the underlying issue is that you do not wish to have direct connections to your server. This is currently the model we're using with Firebase and I can think of two simple patterns to accomplish this.
1) Store the data in an non-guessable path
Create a UUID or GID or, assuming we're not talking bank level security here, just a plain Firebase ID ( firebaseRef.push().name() ). Then have the server and client communicate via this path.
This avoids the need for security rules since the URLs are unguessable, or close enough to it, in the case of the Firebase ID, for normal uses.
Client example:
var fb = new Firebase(MY_INSTANCE_URL+'/connect');
var uniquePath = fb.push();
var myId = uniquePath.name();
// send a message to the server
uniquePath.push('hello world');
From the server, simply monitor connect, each one that connects is a new client:
var fb = new Firebase(MY_INSTANCE_URL+'/connect');
fb.on('child_added', newClientConnected);
function newClientConnected(snapshot) {
snapshot.ref().on('child_added', function(ss) {
// when the client sends me a message, log it and then return "goodbye"
console.log('new message', ss.val());
ss.ref().set('goodbye');
});
};
In your security rules:
{
"rules": {
// read/write are false by default
"connect": {
// contents cannot be listed, no way to find out ids other than guessing
"$client": {
".read": true,
".write": true
}
}
}
}
2) Use Firebase authentication
Instead of expending so much effort to avoid authentication, just use a third party service, like Firebase's built-in auth, or Singly (which supports Firebase). This is the best of both worlds, and the model I use for most cases.
Your client can authenticate directly with one of these services, never touching your server, and then authenticate to Firebase with the token, allowing security rules to take effect.

Resources