GET UUID from PUBNUB hereNOW() using Pubnub V4 - javascript

I am trying to retrieve UUIDs of devices connected to 'my_channel'.
I was using pubnub-3.16.4.js and I was able to get the UUID array using "Here Now".
Now I upgraded to pubnub.4.1.1.js, I followed the example on this site
https://www.pubnub.com/docs/javascript/presence-sdk-v4
when I use Here Now function I do get a response, which includes all state and UUIDs information.
Object {totalChannels: 1, totalOccupancy: 2, channels: Object}
channels:Object
mychannel:Object
name:"my_channel"
occupancy:2
occupants:Array[3]
0: Object
state:null
uuid:"000000003d425f88"
__proto__:Object
1: Object
state:null
uuid:"newmanager" ......................
But what I need is only UUID array, which the sdk example shows.
Any idea how get only UUID array, or is there an easy way to retrieve the UUIDs from the above response?
Thank you.

PubNub Presence Here Now - without state data
pubnub.hereNow(
{
channels: ["ch1"],
includeUUIDs: true,
includeState: false
},
function (status, response) {
// handle status, response
}
);
See full Node SDK hereNow docs for full parameter details and sample code.

Related

How to get all child keys in Firebase?

I have a Firebase (Real-time Database) node with a large number of children. I want to list all child keys, so I used this code:
reference.once('value', snapshot=>{
snapshot.forEach(child => {
console.log(child.key);
})
})
then Firebase will throw an exception:
The specified payload is too large, please request a location with less data.
So I don't know how to get the child keys (only keys) without getting that error. Please help!
Use Firebase's shallow query https://firebase.google.com/docs/database/rest/retrieve-data#shallow
For example, if your data is structured like this
parent: {
child_0: {...<large data>...},
child_1: {...<large data>...},
child_2: "abc",
child_3: 123
}
The shallow query's result will be:
parent: {
child_0: true,
child_1: true,
child_2: "abc",
child_3: 123
}
Some unexpected errors can send a cancel event and terminate the connection. The cause is described in the data provided for this event. Some potential causes are as follows: 1. The Firebase Realtime Database Rules no longer allow a read at the requested location. The data description for this cause is "Permission denied." 2. A write triggered an event streamer that sent a large JSON tree that exceeds our limit, 512MB. The data for this cause is "The specified payload is too large, please request a location with less data."
Since realtime database only limit 500Mb per request so in order to execute this request you should try to paging a query with or try to iterate over children using child_added listener instead of querying all of the childs.

How to access selected elements e.g. title, description, date from rss feed?

I am doing this for learning purpose. I have url to RSS feed that I'd like to work further. This feed contains too much information. I'm interested only in all "item", their "title", "description" and "pubDate". I am using "firebase deploy --only functions" and then checking url for deployment where I expect to see cleaned data. For some reason I am getting error in cloud functions logs: "TypeError: Cannot read property 'channel' of undefined
at cleanUp (/user_code/lib/index.js:19:29)"
I tried this with another URL which surprisingly worked: https://www.theguardian.com/uk/london/rss
Here is the URL to RSS feed I want to use:
https://polisen.se/aktuellt/rss/hela-landet/handelser-i-hela-landet/
Here is my cleanUp function:
function cleanUp(data) {
const items = []
const channel = data.rss.channel
channel.item.forEach(element => {
items.push({
title: element.title,
description: element.description,
date: element.pubDate
})
});
return items
}
I expect to see all items with children title, description and pubdate after deployment. Instead I get "Error: could not handle the request" and when I check my logs in google cloud functions I see:
"TypeError: Cannot read property 'channel' of undefined
at cleanUp (/user_code/lib/index.js:19:29)"
To do so I would recommend working with JSON objects. So what I did is to get the RSS feed and convert it to a JSON object. Then parse item by item and log the data you need.
I have did a little bit of coding my self. Follow the steps below to get an idea of my example and then modify the code according to your needs:
Create a new Google Cloud Function
Trigger: HTTP
Runtime: Node.js 8
Add dependencies in package.json:
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"rss-to-json": "^1.0.4"
}
}
In index.js replace the code with my GitHub code example.
I have tested the code my self and it logged 200 items using the link you provided. Basically I take the RSS and convert it to JSON object. Then I iterate through all the items and log each item's property.

How can I get a value from youtube api?

I'm building a chatbot that retrieves a youtube link based on what the user has asked.
I'm using Dialogflow for the bot and using nodejs for the coding.
Right now, everything works perfectly but my problem is retrieving the link from the API response.
using console.dir(results), shows the following:
This is how the response looks like:
[ { id: 'gWNUg_v25dw',
link: 'https://www.youtube.com/watch?v=gWNUg_v25dw',
kind: 'youtube#video',
publishedAt: '2017-08-24T14:00:11.000Z',
channelId: 'UCDZ56yQ05d_ikcwcTG9bAiA',
channelTitle: 'Code to Create',
title: 'How to make a Chatbot with Dialogflow - API.ai',
description: 'In this video, Google Developer Expert, Faisal Abid will show us how to create a chatbot with Google\'s latest API.ai API.ai can be integrated with multiple chat ...',
thumbnails: { default: [Object], medium: [Object], high: [Object] } } ]
I have tried using results.link to retrieve the link but Im getting undefined, so my question is how to retrieve the link from the response block?
search(txt1, opts, function(err, results) {
//var data1 = JSON.parse(results);
//srchRes = data1.link;
if(err) return console.log(err);
console.dir(results);
});
uncommenting the 2 lines above and console.dir(srchRes) returns this error:
SyntaxError: Unexpected token u in JSON at position 0
We don't have your API call code logic so from here, we could only assume results is the API's response and given your exemple about how the response look like, then we see results is an array of object.
JSON.parse actually deserialize a string which represent an object given the JSON format.
JSON.stringify is the contrary, it serialize your object to a string with the JSON format.
Your goal is to reach the link properties of a particular object within the array, which is why JSON.parse(results) makes no sense.
You should be able to reach linkproperties for each object by simply iterate over the array of object results
for (const result of results) {
console.log(`the link : ${result.link}`)
}

Check an API call for differences

I make an API call and pull down JSON data and store in an Array.
Whats the best way to periodically check if there is a change in the Array that's pulled down and return an Array of the new data?
JSON from API call:
{
success: true,
message: "",
result: [{
Currency: "GBP",
IsActive: true,
BaseAddress: "1N52wHoVR79PMDishab2XmRHsbekCdGquK",
Notice: null
},
{
Currency: "USD",
IsActive: true,
BaseAddress: "LhyLNfBkoKshT7R8Pce6vkB9T2cP2o84hx",
Notice: null
},
{
Currency: "YEN",
IsActive: true,
BaseAddress: "D9GqmkGCpgtnXP7xMD78v9xfqeDkqBZBMT",
Notice: null
}
]
};
Store in an array:
const currencies = [...new Set(data.result.map(a => a.Currency))];
console.log(currencies);
Now say the website add CHF how would I poll the API and get an Array just containing the new data?
Specifically whats the best way to schedule another API call, using setTimeout or is there a better way?
Also, how do I then compare the first array with the second array and return an array containing the difference?
I guess you are making it little bit complex. Best way to achieve this purpose is web sockets.
Web sockets are used to have communication between client and server.
Lets say if you create a server in nodejs and you have a browser client. Web socket will create a communication between server and client.
If there is any changes made by server in your "ARRAY", server can send a notification that a changes has been made in the array so you can make a new request to get the updated array or server can directly send updated array to client.
Advantage of this approach is that there is not need of pull request. Server will push the data so there will be no period check between server and client. This can save a lot of time plus performance.
Please research about web sockets and implement it.

Write an object containing an array of objects to a mongo database in Meteor

In my user collection, I have an object that contains an array of contacts.
The object definition is below.
How can this entire object, with the full array of contacts, be written to the user database in Meteor from the server, ideally in a single command?
I have spent considerable time reading the mongo docs and meteor docs, but can't get this to work.
I have also tried a large number of different commands and approaches using both the whole object and iterating through the component parts to try to achieve this, unsuccessfully. Here is an (unsuccessful) example that attempts to write the entire contacts object using $set:
Meteor.users.update({ _id: this.userId }, {$set: { 'Contacts': contacts}});
Thank you.
Object definition (this is a field within the user collection):
"Contacts" : {
"contactInfo" : [
{
"phoneMobile" : "1234567890",
"lastName" : "Johnny"
"firstName" : "Appleseed"
}
]
}
This update should absolutely work. What I suspect is happening is that you're not publishing the Contacts data back to the client because Meteor doesn't publish every key in the current user document automatically. So your update is working and saving data to mongo but you're not seeing it back on the client. You can check this by doing meteor mongo on the command line then inspecting the user document in question.
Try:
server:
Meteor.publish('me',function(){
if (this.userId) return Meteor.users.find(this.userId, { fields: { profile: 1, Contacts: 1 }});
this.ready();
});
client:
Meteor.subscribe('me');
The command above is correct. The issue is schema verification. Simple Schema was defeating the ability to write to the database while running 'in the background'. It doesn't produce an error, it just fails to produce the expected outcome.

Categories