Notification nodejs - node.js

I use notification in my Web application (nodejs), every time when i refreshe my page /notes i click on my notification and in console i have the message toto. My probleme is : if i refresh seven times my page, in console i have seven messages toto. How can i resolved this probleme.
Thanks
Code :
router.get('/notes', passportConfig.isAuthenticated, function(req, res) {
notifier.notify(
{
title: 'My awesome title',
message: 'Hello from node, Mr. User!',
icon: path.join(__dirname, 'coulson.jpg'),
sound: true,
wait: true
},
function(err, response) {
// Response is response from notification
}
);
notifier.on('click', function(notifierObject, options) {
console.log("toto");
});
});

notifier.on(...) is an event listener. Once is iniciated it will be active until you remove event listener.
Problem is with your function scope. notifier.on() event listener should be iniciated only once.
Try pull off notifier.on() logic from router.get() function scope.
Every time you reloaded page that created another one event listener :)
notifier.on('click', function(notifierObject, options) {
console.log("toto");
});
router.get('/notes', passportConfig.isAuthenticated, function(req, res) {
notifier.notify(
{
title: 'My awesome title',
message: 'Hello from node, Mr. User!',
icon: path.join(__dirname, 'coulson.jpg'),
sound: true,
wait: true
},
function(err, response) {
// Response is response from notification
}
);
});

Related

How to do I make sure that socket.io does not close connections when the app is in background in a Angular PWA

socket.on('new-notification', function(data) {
let room_id = data.room_id;
var receiver_id = data.receiver_id;
var booking_request_id = data.booking_request_id;
var message = {
to: notification_payload,
collapse_key: 'green',
notification: {
title: 'Tittle',
body: "New message from User",
icon: site_url + 'assets/icons/icon- 72x72.png',
sound: 'default',
url: redirect_url,
},
data: {
title: 'Tittle',
body: "New message from User",
icon: site_url + 'assets/icons/icon- 72x72.png',
sound: 'default',
url: redirect_url,
}
};
fcm.send(message, function(err, response) {
if (err) {
console.log("Something has gone wrong!");
} else {
console.log("Successfully sent with response: ", response);
}
});
}
});
}
else {
}
}
});
});
I have an Angular PWA that is using Socket.io Node and express server for real time messaging and push notifications using Firebase Cloud Messaging. Messaging works fine but the problem is that when ever the user goes out of the app they won't be able to receive notifications but only receive when actively on the app. My assumption is that Socket io is disconnecting when the user leaves the app. So is there any way to make sure that socket remains active even if the app is running in the background.
i have attached my notification snippet. Your help will be greatly appreciated
To do this, you will need to set up a service worker for Angular, these run and keep your app alive to do certain tasks when the device would normally put apps into sleep mode.
you can follow the latest Angular documentation on creating service workers HERE

get value from firestore take time

The problem is that the value from the database is updated after the response to google action is finished , i have tried couple of option to make the function to wait for the value to update, but it doesn't work,
i need to execute twice to get the right valuelog
function operation(callback) {
dialogflowAgentDoc.doc(format).get()
.then(doc => {
console.log(doc.data().Url);
Url = doc.data().Url;
})
.catch((err) => {
console.log('Error getting documents', err);
});
callback(Url);
}
app.intent("Default Welcome Intent", conv => {
new GetFormat();
console.log("GetFormat Started");
new operation(function(Url) {
console.log("Ask Started");
conv.ask(
new SimpleResponse({
speech: "Playing Gallay Tzahal",
text: "Playing Gallay Tzahal"
}),
new MediaObject({
name: 'Gallay Tzahal News',
url: Url,
description: "text",
icon: new Image({
url: 'https://upload.wikimedia.org/wikipedia/he/thumb/3/30/GaltzLogo.svg/150px-GaltzLogo.svg.png',
alt: 'Media icon',
}),
})
);
});
conv.ask(suggestions3);
});
Loading data from the cloud takes take. To prevent blocking your app while waiting for that data, the Firestore (and most modern web APIs) load the data asynchronously and then call a function you pass into then() when the data is available.
This means that code that needs the data from Firestore must be inside the then() callback, which gets called when the data is available. So:
function operation(callback) {
dialogflowAgentDoc.doc(format).get()
.then(doc => {
console.log(doc.data().Url);
Url = doc.data().Url;
callback(Url);
})
.catch((err) => {
console.log('Error getting documents', err);
});
}

Twilio not sending status callbacks

I'm trying to get status callbacks for various room events, but twilio is not sending any events to my callback URL, I've used the following code to create a room using the documentation i found here
app.get('/createRoom', function (req, res) {
var client = new Twilio(twilioApiKey, twilioApiSecret, {
accountSid: twilioAccountSid
});
client.video.rooms.create({
uniqueName: myRoomName,
statusCallback: 'myCallbackURL'
}).then((room) => {
//
});
});

Getting list of messages from Twilio with pagenation

I want to get all the messages from my twilio account.
I tried this code
var client = new twilio(twilioConfig.accountSid, twilioConfig.authToken);
client.messages.list({ Page: 0, PageSize: 10 }, function (err, data) {
console.log(data);
res.send({ message: "Success" });
});
in this code getting all messages, i need 10 per page
Twilio developer evangelist here.
If you are using the latest version of the Twilio Node module then you can get all your messages in a couple of ways.
You can call each on the message list object which lazily streams the messages.
const client = require('twilio')(YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN);
client.messages.each({ pageSize: 10 }, function(message) {
console.log(message);
});
Or you can call list on the message list, which eagerly loads all of the messages.
client.messages.list({ pageSize: 10 }, function(messages) {
messages.forEach(function(message) {
console.log(message);
});
});
You can also use page and subsequently nextPage to manually page through all of the messages.
client.messages.page({ pageSize: 10 }, function pageReceived(page) {
page.instances.forEach(function(message) {
console.log(message);
});
if (page.nextPage) {
page.nextPage().then(pageReceived);
}
})
Let me know if any of that helps at all.

How do I output view compiler errors generated from Hapi.js views?

Coming from the world of express, if there was an error when compiling a Jade template, the error would output to the browser with details of the template error including line number. I would like to see this level of error detail in Hapi.js when the view template compiler encounters an error.
Instead, with Hapi.js, I receive a 500 Internal Server Error instead. The only output I see in the logs is the following:
150511/005652.910, [response], http://localhost:3000: get /abc123 {} 500 (24ms)
This is the basics of my setup.
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({ port: 3000 });
server.views({
engines: { jade: require('jade') },
path: __dirname + '/views',
compileOptions: {
pretty: true,
debug: true,
compileDebug: true
}
});
server.route({
method: 'GET',
path: '/{name}',
handler: function (request, reply) {
reply.view('page', {name: request.params.name});
}
});
// I know putting my plugins in an array is not necessary but I like it.
var plugins = [{
register: Good,
options: {
reporters: [{
reporter: require('good-console'),
events: {
response: '*',
log: '*'
}
}]
}
}];
server.register(plugins, function (err) {
if (err) {
throw err; // something bad happened loading the plugin
}
server.start(function () {
server.log('info', 'Server running at: ' + server.info.uri);
});
});
Had the same problem this week too.
var server = new Hapi.Server({
debug: {
request: ['error']
}
});
It won't output to the client (you'll still get a 500 error), but the compilation error will show up in the terminal console.
One possible solution is to log on server 'request-error'. For instance:
server.on('request-error', function (request, err) {
//logs the object
server.log('error', err);
//logs the view compiler error line number and details
server.log('error', err.toString());
});
I would still prefer to see this in the browser (while in "development mode") in addition to the logs.
Because the rendering of the template happens after onPreResponse, it's not possible to catch the error at that point. A way of sending the error to the browser, albeit slightly hacky, is to do a dry run of the compilation inside an extension point, and then transmit the error to the browser at that point:
server.ext('onPreResponse', function (request, reply) {
var response = request.response;
if (response.variety === 'view') {
var source = response.source;
// Let's pre-render the template here and see if there's any errors
return server.render(source.template, source.context, function (err) {
if (err) {
return reply(err.message); // transmit the compile error to browser
}
reply.continue();
});
}
reply.continue();
});
Obviously this has a performance impact because you're rendering the view twice under normal conditions, so you'll want to disable it in production.

Resources