Save location with geofire on an existing document - firebase

I want an using firebase real time database to store events of some sort.
An event can have a changing status in its life cycle.
I want to be able to fetch all active events near a user current location.
Because it doesn't seems like geofire enables me to save the location on the event document, I would have to get all events near the location, and then filter by status and given event ids (is that event possible??).
Is there a better way?
Thanks!

Geofire works by returning what's close to coordinates you give it. For an idea or starter template, check out https://getaa.org. (click the map icon top center and enter 'san francisco' or 'baton rouge' if you are in an area with no meetings.
The code is all on github.
Here's a code snippet:
function loadQuery(lat,lng,today){
//console.log('loadQuery function fired with ' + lat,lng,today);
var geoKeys = [];
var records=[];
var geoQuery = geofire.query({
center: [lat,lng],
radius: radius
});
var onKeyEnteredRegistration = geoQuery.on("key_entered", function(key, location, distance) {
geoKeys.push(key);
//console.log('geoQuery event KEY ENTERED with key: ' + key);
});
// GeoKeys now in array
var onReadyRegistration = geoQuery.on("ready", function() {
//console.log('geoQuery event READY');
if(geoKeys.length > 0){
toastUp('Fetching Meetings...');
// Get recordset for each key into sites array
readFirebaseNodes(geoKeys).then(function(value) {
//filter for today
var todaysMeetings = dayFilter(today);
drop(todaysMeetings);
}, function(err) {
//console.log(err); // Error!
});
} else {
toastUp('No area meetings found. You are encouraged to volunteer to add them. Click Meetings Manager to become a site administrator.');
toastDown(2000);
}
});
}

The Firebase Realtime Database can only query on a single property. The fact that GeoFire can filter on two axes (longitude and latitude) is already quite magical (thanks to the use of geohashes). Adding one more value to the mix is beyond what GeoFire is made for.
But it's actually quite simple to only show nearby active events: keep a location/reference in your database with just the locations of those active events. Then you can simply create a GeoFire object on that location, and it will only find nearby active events:
// Create a Firebase reference where GeoFire will store its information
var firebaseRef = firebase.database().ref("activeEventLocations");
// Create a GeoFire index
var geoFire = new GeoFire(firebaseRef);

Related

i need to get the system time & store it as a child value in firebase

I need to add current system time into child data field.
I'm using TypeScript, but this might still give you and idea how you could do it.
My code uses the event.timestamp property to get date and time:
export const onWrite = functions.database.ref('/{databaseName}/{tableName}/{key}').onCreate((event) => {
let ref = event.data.ref;
let isCreate = event.eventType == 'providers/google.firebase.database/eventTypes/ref.create';
ref.once('value').then(e => {
// Cloud functions are sometimes executed with a delay and the record might not exist anymore.
if (e.val() === null) {
return;
}
if (isCreate) {
return ref.update({
'createdDateTime': event.timestamp
});
}
});
});
The created events for clients won't include this added data yet, only a later change event does.
I'm haven't investigated yet if this can be fixed (perhaps by making use of transaction).
I saw your image description and understood u want to add system time into firebase.
If you want to do you can do that by , like below
var fb_db=firebase.database().ref('treeName');
var key=fb_db.push().key;
var updatenode={}
updatenode[key+"\"]= new Date();
fb_db.update(updatenode).then(function(){
alert("Success")
})

GeoFire retrieve all queries within a range when moving.

I'm trying to retrieve all elements from my GeoFire database when moving. For example: when walking around I want to retrieve in realtime the locations of elements around me (moving or not moving).
If I use the following code:
var geoQuery = geoFire.query({
center: [52.35500, 4.931000],
radius: 0.1 //kilometers
});
var onKeyEnteredRegistration = geoQuery.on("key_entered", function(key, location, distance) {
console.log(key + " entered query at " + location + " (" + distance + " km from center)");
});
I only receive updates when the keys are changing position (and entering my query). Is there a possibility to retrieve all elements within a certain range as a sort of a snapshot? And from there monitor realtime?
I could of course query the whole database and then use
GeoFire.distance(location1, location2)
But this looks like a very expensive option.
You can call GeoQuery.updateCriteria(newQueryCriteria) to update the center of the query as you move.
Note that you probably want to register a key_exited callback as well.
Check out the GeoFire API Reference
You don't need to query whole database. You can store your places in a variable and calculate new distances when your current position (query center) changes.
private places: any;
var onKeyEnteredRegistration = geoQuery.on("key_entered", function(key, location, distance) {
this.places.push({ key, location, distance });
});
var onKeyExitedRegistration = geoQuery.on("key_exited", function(key, location, distance) {
this.places = this.places.filter(place => place.key !== key);
});
updatePlacesDistance(currentLocation) {
this.places.map(place => {
place.distance = GeoFire.distance(currentLocation, place.location);
});
}

how to discard initial data in a Firebase DB

I'm making a simple app that informs a client that other clients clicked a button. I'm storing the clicks in a Firebase (db) using:
db.push({msg:data});
All clients get notified of other user's clicks with an on, such as
db.on('child_added',function(snapshot) {
var msg = snapshot.val().msg;
});
However, when the page first loads I want to discard any existing data on the stack. My strategy is to call db.once() before I define the db.on('child_added',...) in order to get the initial number of children, and then use that to discard that number of calls to db.on('child_added',...).
Unfortunately, though, all of the calls to db.on('child_added',...) are happening before I'm able to get the initial count, so it fails.
How can I effectively and simply discard the initial data?
For larger data sets, Firebase now offers (as of 2.0) some query methods that can make this simpler.
If we add a timestamp field on each record, we can construct a query that only looks at new values. Consider this contrived data:
{
"messages": {
"$messageid": {
"sender": "kato",
"message": "hello world"
"created": 123456 // Firebase.ServerValue.TIMESTAMP
}
}
}
We could find messages only after "now" using something like this:
var ref = new Firebase('https://<your instance>.firebaseio.com/messages');
var queryRef = ref.orderBy('created').startAt(Firebase.ServerValue.TIMESTAMP);
queryRef.on('child_added', function(snap) {
console.log(snap.val());
});
If I understand your question correctly, it sounds like you only want data that has been added since the user visited the page. In Firebase, the behavior you describe is by design, as the data is always changing and there isn't a notion of "old" data vs "new" data.
However, if you only want to display data added after the page has loaded, try ignoring all events prior until the complete set of children has loaded at least once. For example:
var ignoreItems = true;
var ref = new Firebase('https://<your-Firebase>.firebaseio.com');
ref.on('child_added', function(snapshot) {
if (!ignoreItems) {
var msg = snapshot.val().msg;
// do something here
}
});
ref.once('value', function(snapshot) {
ignoreItems = false;
});
The alternative to this approach would be to write your new items with a priority as well, where the priority is Firebase.ServerValue.TIMESTAMP (the current server time), and then use a .startAt(...) query using the current timestamp. However, this is more complex than the approach described above.

Prevent items in scope from writing to a different user's records

I was having success with using AngularFire in a scenario where there is one user on my application.
Now that I have authentication up and running, I'm noticing that assigning items to $scope.items is catastrophic when switching users, mainly due to the $scope failing to update correctly.
Reading directly from the docs...
var ref = new Firebase('https://<my-firebase>.firebaseio.com/items');
angularFire(ref, $scope, 'items');
I need these to be only the items of the currently authorized user. So currently, I do this (if there's a better way, don't hesitate to tell me!)
var ref = new Firebase('https://<my-firebase>.firebaseio.com/items/userId');
angularFire(ref, $scope, 'items');
I generate userId using auth.provider and auth.id, btw. Now that my items are namespaced in (let's say) user1
var ref = new Firebase('https://<my-firebase>.firebaseio.com/items/[user1id]');
angularFire(ref, $scope, 'items');
I add items to $scope.items
$scope.create = function(item) {
$scope.items.push(item)
/* Pretend the user adds these from the interface.
[
{ name: 'eenie' },
{ name: 'meenie' },
{ name: 'miney' },
{ name: 'moe' }
]
*/
}
The problem
Now if I just log out and login as someone else, magically that user has eenie meenie miney and moe because $scope.items held the array between logout and login.
I tried to set $scope.items = [] on logout event, but that actually empties all the records. I'm pulling my hair out. This is 0.001% of what I need to do in my project and it's taking my whole weekend.
Update New method
$scope.create = function() {
$scope.selectedDevice = {
name: 'New Device',
userId: $scope.user.provider + $scope.user.id
};
return $scope.devices.push($scope.selectedDevice);
};
$scope.$on('angularFireAuth:login', function(evt, user) {
var promise, ref;
ref = new Firebase('https://mysite.firebaseio.com/users/' + (user.provider + user.id) + '/registry/');
promise = angularFire(ref, $scope, 'devices');
});
It now will accurately create items under the user's id. However, still, once you logout and log back in, those items do not get cleared from $scope.devices. Therefore, they just add themselves to data but under the newly logged in user.
Update
I did a lot of trial and error. I probably set $scope.devices to [] and moved around login events in every possible combination. What eventually worked was #hiattp's fiddle in the accepted answer.
This is a result of the implicit data binding remaining intact as you switch users. If the new user shows up and creates a new binding, it will consider the existing data to be local changes that it should assimilate (that's why you see the original user's items being added to the new user), but if you try to clear them first without releasing the binding then you are implicitly telling Firebase to delete that data from the original user's item list (also not what you want). So you need to release the data bindings when you detect the logout (or login) events as needed.
The callback in the angularFire promise provides an "unbind" method (see here and here):
var promise = angularFire(ref, $scope, 'items');
promise.then(function(unbind){
// Calling unbind() will disassociate $scope.items from Firebase
// and generally it's useful to add unbind to the $scope for future use.
});
You have a few idiosyncrasies in your code that are likely causing it not to work, and remember that unbind won't clear the local collection for you. But just so you have an idea of how it should work (and to prove it does work) here is a fiddle.
You need to unbind $scope.items on logout. The best way to do this will be to save the unbind function given to your promise in $scope:
var ref = new Firebase('https://<my-firebase>.firebaseio.com/items/[user1id]');
angularFire(ref, $scope, 'items').then(function(unbind) {
$scope.unbindItems = unbind;
});
$scope.$on('angularFireAuth:logout', function() {
$scope.unbindItems();
});

How to retrieve only new data?

I'm trying to create simple notification system for my site admin, and I need to send only real-time messages to every admin user. But when I use firebase it loads old data on every page, and user see all messages from database. If I set limit(1) user will see last notification on every page reloading:
var eventsList = new Firebase('https://*****-messages.firebaseio.com/');
eventsList.on('child_added', function(message) {
var message = message.val();
$.notification(message.message);
});
How I can load only new messages, without old notification history?
This is by design, in a real-time system there is no concept of the "latest" data because it's always changing. However, if you want to only display items added to the list after the page has loaded, you can do the following:
var newItems = false;
var eventsList = new Firebase('https://*****-messages.firebaseio.com/');
eventsList.on('child_added', function(message) {
if (!newItems) return;
var message = message.val();
$.notification(message.message);
});
eventsList.once('value', function(messages) {
newItems = true;
});
I would comment on the above, but due to reputation I cannot, so hoping this is adequate and this is to address the last comment by Gruff McGruff.
Once fires after because you want to break out of the loop of grabbing all child items. Once that loops is broken, you'll set the newItems variable to true, and then it will be able to get all new children after that.
If you fired it before, it would defeat the purpose and grab all child items regardless because you'll set the newItems variable immediately.
Also, I've used this approach and it works well.

Resources