Firebase - Get last 100 response onValue trigger? - firebase-realtime-database

I need to get the last 100 response whenever onValue triggers, however I am not sure if using callback method will anyway query for all the results as shown below:
onValue(chatRef, (snapshot) => {
if (snapshot.exists()) {
console.log('Added child = ', snapshot.val());
} else console.log('Uhum');
});
In the above snippet, my understanding is that snapshot.val being a synchronous method would have already fetched all the response which can be troublesome for me. However how I want it to be is:
onValue(query(chatRef, limitToLast(100)), async (snapshot) => {
if (snapshot.exists()) {
console.log('Added child 01 = ', snapshot.val());
} else console.log('Uhum');
});
My question is will the second call make a difference or performance and resource consumption wise both are the same? And also will the second call ensure that although I am taking last 100 entries but it will be watching all the records that get updated or added?

Related

Firestore batch.commit adding more than 500 documents at a time

I am new to Firestore, trying to figure out a fast way to add some documents in Firestore using Dart.
I used the code below. I had about 3000 strings in a list, and it ended up adding all the 3000 documents, but it took a long time, about 10 minutes, and I also got an error message after batch.commit, that the 500 limit was exceeded, even though it added all 3000 documents.
I know I should break it down into 500 at a time, but the fact that it added all the documents in the list does not make sense to me. I checked in Firestore Console, and all the 3000 documents were there.
I need to create a document id every time I add a document. What am I doing wrong? Is it ok to use the add to get a document reference, and then batch.setData?
Future<void> addBulk(List<String> stringList) async {
WriteBatch batch = Firestore.instance.batch();
for (String str in stringList) {
// Check if str already exists, if it does not exist, add it to Firestore
if (str.isNotEmpty) {
QuerySnapshot querySnapshot = await myCollection
.where('name', isEqualTo: str)
.getDocuments(source: Source.cache);
if (querySnapshot.documents.length == 0) {
UserObject obj = UserObject(name: str);
DocumentReference ref = await myCollection.add(obj.toMap());
batch.setData(ref, UserObject.toMap(), merge: true);
}
}
}
batch.commit();
}
Your code is actually just adding each document separately, regardless of what you're doing with the batch. This line of code does it:
DocumentReference ref = await myCollection.add(obj.toMap());
If you remove that line (which, again, is not touching your batch), I'm sure you will just see a failure due to the batch size.
If you are just trying to generate a unique ID for the document to be added in the batch, use document() with no parameters instead:
DocumentReference ref = myCollection.document();

How to check if a record was updating using Zend Framework's 2 Sql Adapter Class

I'm trying to test to see if an update query was successful with Zend Framework 2. I'm using the getAdapter()->query() methods but I'm unsure of how to actually test to see if anything was returned or if it actually executed. I know it is executing (as I can see the update working via mysql workbench) but I'm not sure on how to actually count or verify. Here is the code I have in place (which I know is wrong but I don't know what else to do):
$update = $this->update->table('stores')
->set(array('number_of_items' => $number))->where(array('store_name' => $this->store_name));
$query = $this->sql->getAdapter()->query($this->sql->buildSqlString($update), Adapter::QUERY_MODE_EXECUTE);
if ($query->count() > 0) {
// insert the items into the items table
$insert = $this->insert->into('items')
->columns(array('store_id', 'price', 'description'))
->values(array($row['store_id'], $price, $item_desc));
$query = $this->sql->getAdapter()->query(
$this->sql->buildSqlString($insert),
Adapter::QUERY_MODE_EXECUTE
);
if ($query->count() > 0) {
return true;
} else {
throw new \Exception("Error adding your item to the items table, please try again.");
}
} else {
// this is the exception being thrown
throw new \Exception("An error occurred while adding your item(s) to the store, please try again");
}
Now I know most likely count() will only work on select queries but I am unsure of how to test to see if the update and insert were successful.
Any help would be appreciated
Thanks
To test if update and insert were successful.
As per your code
try {
$affetedRows = $this->insert->into('items')
->columns(array('store_id', 'price', 'description'))
->values(array($row['store_id'], $price, $item_desc));
}catch (\Exception $e) {
var_dump($e->getMessage());exit; // see if any exaption Or error in query
}
}
var_dump($affetedRows ) // it will return number of affected rows.
Same for delete and update, after successfull execution delete and updateare also returns number of affected rows.
so if there is successfull exceution, you can check success of your query.
Thanks.

Atomic update of Realtime Database from Google Cloud Functions

I use Google Cloud Functions to create an API endpoint for my users to interact with the Realtime Database.
The problem I have is that I'm not sure how the code works. I have a helper function doSomething that I need to call only once, but I have a suspicion that there are cases where it can be called twice or possibly more (when multiple users call the API at the same time and the update operation hasn't been processed by the DB yet). Is it possible? Does it mean I need to use a transaction method? Thank you!
DB structure
{
somePath: {
someSubPath: null
}
}
Google Cloud Functions code
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const cors = require('cors')({origin: true});
admin.initializeApp(functions.config().firebase)
// API ENDPOINT
exports.test = functions.https.onRequest((req, res) => {
cors(req, res, () => {
admin.database().ref('/somePath/someSubPath').once('value')
.then(snapshot => {
const value = snapshot.val()
if (value) return res.status(400).send({ message: 'doSomethingAlreadyCalled' })
doSomething()
const updates = { '/somePath/someSubPath': true }
return admin.database().ref().update(updates)
.then(() => res.status(200).send({ message: 'OK' }))
})
.catch(error => res.status(400).send({ message: 'updateError' }))
})
})
// HELPERS
const doSomething = () => {
// needs to be called only once
}
I believe you were downvoted due to the above pseudocode not making complete sense and there being no log or output of what your code is actually doing in your question. Not having a complete picture makes it hard for us to help you.
Just Going from your structure in the question, your actual code could be calling twice due to function hoisting. Whenever I have this issue, I’ll go back to the api documentation and try to restructure my code from rereading.
HTH

Is it possible to post data to couch db and return data?

For example I would like to send the users score to the database and instead of it returning the typical status, id and rev I would like it to return the users rank. I'm guessing this isn't possible but figured I would ask.
The response to an HTTP POST/PUT should really only be used to help you confirm that it succeeded.
I'm even struggling to see even how you can get the rank of a user returned by a couchdb view, unless you retrieve the data for all users and work out the position of your user.
This use case ...
Simple structured data clearly tabular
The requirement to respond fast to a numerical column (Method to calculate the rank for a score)
OR the requirement to trigger an update a score table each time a rank is submitted.
... very much smells like a classical case where you may want to use a relational DB.
If the result can be calculated from the document you are to change with your http request, then you can use an update handler to PUT a change to the document and return that result:
// 'myhandler' update function
function(doc, req) {
// create a shorthand for json reponses
var json_reponse = function(obj, code) {
return {
headers: { 'Content-Type': 'application/json' }
, body: JSON.stringify(obj)
, code: code
}
}
// assume the incoming body is json and parse it
// needs proper error handling still
var body = JSON.parse(req.body)
// doc is the user document we are patching
// return an error if it isn't there
if(!doc)
return [null, json_response({error: 'user document not found'}, 404)]
// return an error if new_score is missing from body
if(!body.new_score)
return [null, json_response({error: 'missing property new_score'}, 400)
// now patch the user doc
doc.score = body.new_score
// calculate the new rank depending on your own method
var my_rank = my_rank_function(doc.score, Math.PI, 'bananarama')
return [doc, json_response({success: true, rank: my_rank}, 200)
}
Now PUT new data to receive the new rank:
request(
{ method: 'PUT'
, url: httptp://127.0.0.1:5984/mydb/_design/myddoc/_update/myhandler/myuserdocid
, json: {"new_score": 42}
, headers: { "Content-Type: application/json" }
}
, function(err, response, body) {
console.log("user's new rank:", JSON.parse(body).rank)
}
)
should print user's new rank: LEVEL 11 EIGHTIES GIRL GROUP LEADER
nb: I'm not at work so cannot confirm the code works, but you should get the hang of it...

ASP.NET MVC ajax chat

I built an ajax chat in one of my mvc website. everything is working fine. I am using polling. At certain interval i am using $.post to get the messages from the db. But there is a problem. The message retrieved using $.post keeps on repeating. here is my javascript code and controller method.
var t;
function GetMessages() {
var LastMsgRec = $("#hdnLastMsgRec").val();
var RoomId = $("#hdnRoomId").val();
//Get all the messages associated with this roomId
$.post("/Chat/GetMessages", { roomId: RoomId, lastRecMsg: LastMsgRec }, function(Data) {
if (Data.Messages.length != 0) {
$("#messagesCont").append(Data.Messages);
if (Data.newUser.length != 0)
$("#usersUl").append(Data.newUser);
$("#messagesCont").attr({ scrollTop: $("#messagesCont").attr("scrollHeight") - $('#messagesCont').height() });
$("#userListCont").attr({ scrollTop: $("#userListCont").attr("scrollHeight") - $('#userListCont').height() });
}
else {
}
$("#hdnLastMsgRec").val(Data.LastMsgRec);
}, "json");
t = setTimeout("GetMessages()", 3000);
}
and here is my controller method to get the data:
public JsonResult GetMessages(int roomId,DateTime lastRecMsg)
{
StringBuilder messagesSb = new StringBuilder();
StringBuilder newUserSb = new StringBuilder();
List<Message> msgs = (dc.Messages).Where(m => m.RoomID == roomId && m.TimeStamp > lastRecMsg).ToList();
if (msgs.Count == 0)
{
return Json(new { Messages = "", LastMsgRec = System.DateTime.Now.ToString() });
}
foreach (Message item in msgs)
{
messagesSb.Append(string.Format(messageTemplate,item.User.Username,item.Text));
if (item.Text == "Just logged in!")
newUserSb.Append(string.Format(newUserTemplate,item.User.Username));
}
return Json(new {Messages = messagesSb.ToString(),LastMsgRec = System.DateTime.Now.ToString(),newUser = newUserSb.ToString().Length == 0 ?"":newUserSb.ToString()});
}
Everything is working absloutely perfect. But i some messages getting repeated. The first time page loads i am retrieving the data and call GetMessages() function. I am loading the value of field hdnLastMsgRec the first time page loads and after the value for this field are set by the javascript.
I think the message keeps on repeating because of asynchronous calls. I don't know, may be you guys can help me solve this.
or you can suggest better way to implement this.
Kaivalya is correct about the caching, but I'd also suggest that your design could and should be altered just a tad.
I made a very similar app recently, and what I found was that my design was greatly enhanced by letting the controllers work with the fairly standard PRG pattern (post-redirect-get). Why enhanced? well, because POST methods are built to add stuff to an app, GET methods are supposed to be used to get information without side effects. Your polling should be just getting new messages w/o side effects.
So rather than your $.post call expecting data and handling the callback, what I'd recommend is having your controller expose a method for creating new chat messages via POST and then another method that get the last X chat messages, or the messages since a certain timestamp or whatever.
The javascript callback from the post action, then can update some variables (e.g. the last message id, timestamp of the last message, or even the whole URL of the next message based on the info contained in a redirect, whatever).
The $.post would fire only in response to user input (e..g type in a box, hit 'send') Then, you have (separately) a $.get call from jquery that's set up to poll like you said, and all it does is fetch the latest chat messages and it's callback updates the chat UI with them.
I got my answer here: ASP.NET AJAX CHAT
The names below i am referring to are from above link.
i think the actual problem was with the timestamp thing and asynchronous behaviour of $.post. after calling "GetMessages()" method, even if the previous request to retrive chat message was not complete anathor call to same method used to fire due to setting timeout for "GetMessages()" method outside the $.post method. In my question you can see that timeout for "GetMessages()" method is set outside the $.post method. Now i set the timeout for "GetMessages()" method inside the $.post method. so that next call to "GetMessages()" only occur after 3 seconds of completion of current $.post method. I have posted the code below.
var t;
function GetMessages() {
var LastMsgRec = $("#hdnLastMsgRec").val();
var RoomId = $("#hdnRoomId").val();
//Get all the messages associated with this roomId
$.post("/Chat/GetMessages", { roomId: RoomId, lastRecMsg: LastMsgRec }, function(Data) {
if (Data.LastMsgRec.length != 0)
$("#hdnLastMsgRec").val(Data.LastMsgRec);
if (Data.Messages.length != 0) {
$("#messagesCont").append(Data.Messages);
if (Data.newUser.length != 0)
$("#usersUl").append(Data.newUser);
$("#messagesCont").attr({ scrollTop: $("#messagesCont").attr("scrollHeight") - $('#messagesCont').height() });
$("#userListCont").attr({ scrollTop: $("#userListCont").attr("scrollHeight") - $('#userListCont').height() });
}
else {
}
t = setTimeout("GetMessages()", 3000);
}, "json");
}
I addition to that i also changed few things. As suggested by ignatandrei i placed $("#hdnLastMsgRec").val(Data.LastMsgRec); immediately after function(Data) {.
and also
as said by MikeSW i changed the data retrieval process. Previously i was extracting data on the basis of timespan(retrieve all the data associated with
this room id that has greater timespan than last data retrieved message timespan) but now i keep track of the messageid. Now i retrieve only those data that
has message id greater than last retrieved message id.
and guess what no repeataion and perfectly working chat application so far on my intranet.
I still got to see it's performance when deployed on internet.
i think it solved my problem.
i will still test the system and let u guys know if there is any problem.
By default $.post() caches the results
You can either call $.ajaxSetup ({ cache: false}); before JS GetMessages function call to ensure caching is disabled or change the $.post to $.ajax and set cache attribute to false. In the end $.post() is a short cut to this:
$.ajax({
type: 'POST',
url: url,
data: data,
success: success
dataType: dataType
});

Resources