I have a Firebase cloud function, that is looping through users in a database and sends an email to each one. It uses the Campaign Monitor API for Transactional/Smart Emails, but the request results in an ESOCKETTIMEDOUT error. Campaign Monitor receives and processes each request just fine, but I would like to avoid the error of course.
How can I do this? I've read elsewhere about a setting, 'agent: false, pool: {maxSockets: 100}', but I am not sure if this is the right approach - and I don't know where to set it on a Firebase og Google Cloud Function.
This is the request snippet, that is called by the loop.
api.transactional.sendSmartEmail(details, function (err, res) {
if (err) {
console.log('error sending mail, err);
/*Throws:
{ Error: ESOCKETTIMEDOUT
at ClientRequest.<anonymous> (/user_code/node_modules/createsend-node/node_modules/request/request.js:813:19)
at ClientRequest.g (events.js:292:16)
at emitNone (events.js:86:13)
at ClientRequest.emit (events.js:185:7)
at TLSSocket.emitTimeout (_http_client.js:630:10)
at TLSSocket.g (events.js:292:16)
at emitNone (events.js:86:13)
at TLSSocket.emit (events.js:185:7)
at TLSSocket.Socket._onTimeout (net.js:338:8)
at ontimeout (timers.js:386:11) code: 'ESOCKETTIMEDOUT', connect: false }
*/
} else {
console.log('sent mail');
}
});
Related
I am fairly new to Node and am currently trying to tweak an existing project. Part of this uses the follow-redirects package to make HTTP(S) requests to fetch an image.
Occasionally my service crashes with the following trace:
events.js:174
throw er; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (_http_client.js:323:15)
at Socket.socketOnEnd (_http_client.js:426:23)
at Socket.emit (events.js:203:15)
at Socket.EventEmitter.emit (domain.js:448:20)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at ClientRequest.eventHandlers.(anonymous function) (/home/.../node_modules/follow-redirects/index.js:13:24)
at ClientRequest.emit (events.js:198:13)
at ClientRequest.EventEmitter.emit (domain.js:448:20)
at Socket.socketOnEnd (_http_client.js:426:9)
at Socket.emit (events.js:203:15)
[... lines matching original stack trace ...]
at process._tickCallback (internal/process/next_tick.js:63:19)
I assume this means that I am dropping the error which comes back from the HTTP request.
Here is my function where I make the request:
function fetchRemote (imageUrl) {
let options = ...
return new Promise((resolve, reject) => {
https.get(options, resolve);
});
}
And it's called using a chain of promises.
fetchRemote(imageUrl)
.then(getResizeStream)
.then(storeImage)
.then(finished)
.catch(error);
function error (err) {
reject(new Error(`Failed: ${err.message}`));
};
Is there a logic pathway I'm missing, or should this properly catch the error? Or is the issue actually in the follow-redirects package?
Sockets are "event" emitter based and not promises. You need to handle errors on sockets by doing something along the lines of
socket.on('error',function(err){ /* handle error */});
// for you:
https
.get(options, resolve)
.on('error', err => {
reject(err);
});
At first, I installed this library,
> npm install rocketchat-api
and then I've ran this code to use rocket.chat REST API
// Node.js code
const RocketChatApi = require('rocketchat-api');
const result='https://rocket.chat/docs/developer-guides/rest-api/users/setavatar';
const userId='A user id';
const avatarUrl='http://www.coca.ir/wp-content/uploads/2017/05/flowers-love-profile-pictures-1.jpg'
const rocketChatClient = new RocketChatApi(
'https',
'rocket.chat',
443,
'username',
'password',
(err, result)=>{
console.info('RC connected', result)
});
rocketChatClient.users.setAvatar(userId, avatarUrl, (err, body)=>{
});
After run this in Node.js, I got following error, anyone can help me?
\node_modules\rocketchat-api\lib\net.js:144
return callback ? callback(error, null) : reject(error);
^
TypeError: callback is not a function
at Request.clientRequest [as _callback] (D:\node js projects\rocketTest\node_modules\rocketchat-api\lib\net.js:144:39)
at self.callback (D:\node js projects\rocketTest\node_modules\request\request.js:185:22)
at Request.emit (events.js:198:13)
at ClientRequest.<anonymous> (D:\node js projects\rocketTest\node_modules\request\request.js:819:16)
at Object.onceWrapper (events.js:286:20)
at ClientRequest.emit (events.js:198:13)
at TLSSocket.emitRequestTimeout (_http_client.js:662:40)
at Object.onceWrapper (events.js:286:20)
at TLSSocket.emit (events.js:198:13)
at TLSSocket.Socket._onTimeout (net.js:442:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
That's because setAvatar() only accepts 2 parameters: avatarUrl and callback. If you pass 3 parameters, it would take the second one as callback, and throw TypeError if it is not a function (In your example, the 2nd parameter avatarUrl is a string).
Please refer to the source code of lib/api/users.js in rocketchat-api source code:
setAvatar (avatarUrl, callback) {
return this.client.request("POST", "users.setAvatar", {
avatarUrl
}, callback);
}
Unfortunately, the rocketchat-api module's document is incorrect, which leads the problem.
I set up a one firebase function to send messages to my bot. But when firebase gets request I can see that some error occurred.
FetchError: request to https://api.telegram.org/bot<BOT-TOKEN>/getMe failed, reason: getaddrinfo EAI_AGAIN api.telegram.org:443
at ClientRequest.<anonymous> (/srv/node_modules/node-fetch/lib/index.js:1453:11)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at TLSSocket.socketErrorListener (_http_client.js:401:9)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:66:8)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
and
Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (/worker/node_modules/express/lib/response.js:767:10)
at ServerResponse.contentType (/worker/node_modules/express/lib/response.js:595:15)
at ServerResponse.sendStatus (/worker/node_modules/express/lib/response.js:357:8)
at /srv/index.js:30:13
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)
But secound one I suppose does not influence on the result.
As a result, I don't get any message to my to Bot.
The next script that i use:
const functions = require('firebase-functions');
const telegraf = require('telegraf');
const axios = require('axios');
const bot = new telegraf('<BOT-TOKEN>')
bot.start((ctx) => ctx.reply('Welcome!'))
bot.help((ctx) => ctx.reply('Send me a sticker'))
bot.on('sticker', (ctx) => ctx.reply('👍'))
bot.hears('hi', (ctx) => ctx.reply('Hey there'))
bot.launch()
exports.helloWorld = functions.https.onRequest((request, res) => {
const token = '<BOT-TOKEN>';
const url = `https://api.telegram.org/bot${token}/sendMessage`;
axios.post(url, {
chat_id: '154866113',
text: "just do it!"
})
.then(function (response) {
console.log(response);
res.send({ status: "O"});
return 'ok';
})
.catch(function (error) {
console.log(error);
res.sendStatus(500);
});
res.send("Hello from Firebase!");
});
The getaddrinfo EAI_AGAIN error means that you need to switch to the "Flame" or "Blaze" pricing plan.
As a matter of fact, the free "Spark" plan "allows outbound network requests only to Google-owned services". See https://firebase.google.com/pricing/ (hover your mouse on the question mark situated after the "Cloud Functions" title)
The telegram API is not a Google-owned service, so you need to switch to the "Flame" or "Blaze" plan.
I'm guessing the answer to the EAI_AGAIN error is due to a malformed URI. Are you sure https://api.telegram.org/bot<BOT-TOKEN>/getMe is the correct URI and is accessible from where ever this script is being run from?
The second error is due to you calling the res.send() method more than once. Express res objects wrap Node's ServerResponse object, and once the connection is closed via write or end, it cannot be sent again. Try removing the res.send("Hello from Firebase!"); section of your code and coming back.
I'm using the node googleapis library to make requests to the youtube data api. I'm starting of with authenticating a user using passport the passport-youtube-v3 library. Everything works fine. I'm able to authenticate and I can make authorized requests to the youtube data api. But after a certain amount of time (around 1-2h) the credentials seem to have expired or just become invalid and I get the following error:
{ Error: Invalid Credentials
at Request._callback (/Users/flavio/Code/BA/node_modules/google-auth-library/lib/transporters.js:85:15)
at Request.self.callback (/Users/flavio/Code/BA/node_modules/request/request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1091:12)
at IncomingMessage.g (events.js:286:16)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:926:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
code: 401,
errors:
[ { domain: 'global',
reason: 'authError',
message: 'Invalid Credentials',
locationType: 'header',
location: 'Authorization' } ] }
I understand that auth tokens expire after some time. But according to the documentation this shouldn't happen before 6 months of inactivity. Also I'm not making an excessive amount of requests that could render the tokens invalid. Nevertheless I tried to implement a manual toke refresh of the access token like this:
const fetch = (user, ressource, operation, opts) => {
let oauth2Client = new OAuth2();
let client = google.youtube({ version: 'v3',auth: oauth2Client })[ressource][operation];
oauth2Client.credentials = {
access_token: user.youtube.token,
refresh_token: user.youtube.refreshToken
};
return new Promise((success, failure) => {
client(opts, function(err, data, response) {
if (err)
if(err.code === 401)
oauth2Client.refreshAccessToken(function(err, tokens) {
console.log(err);
});
else
failure(err);
if (data)
success(data);
});
});
}
I'm not sure if I implemented this correctly of if it even makes sense to do this. I get the following error:
{ Error: invalid_request
at Request._callback (/Users/flavio/Code/BA/node_modules/google-auth-library/lib/transporters.js:81:15)
at Request.self.callback (/Users/flavio/Code/BA/node_modules/request/request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1091:12)
at IncomingMessage.g (events.js:286:16)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:926:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9) code: 400 }
What else could be the issue when getting the first error? Is it possible that the access token expires so quickly? If so, how do I refresh it correctly?
Any help is greatly appreciated!
It seems like the problem was that I didn't instantiate OAuth2 correctly. I didn't pass the credentials of my app to the constructor upon creation. Everything seems to work now.
let oauth2Client = new OAuth2(
clientID,
clientSecret,
callbackURL
);
The only confusion I still have is as to why it worked for a couple of requests before it started to throw the error described above.
I am building a web application using Firebase and the new feature Cloud Functions for Firebase. I have created a function that takes a URL and downloads the image into a 64-bit encoded string as below using the node modules request and request-promise-native:
module.exports = {
downloadImageFromUrl: function (url) {
var options = {
method: 'GET',
uri: url,
resolveWithFullResponse: true,
simple: false,
family: 4
};
return rp.get(options)
.then(function (res) {
return "data:" + res.headers["content-type"] + ";base64," + new Buffer(res.body).toString('base64');
})
.catch(function (error) {
console.log("ERROR GETTING image", error);
return error;
});
}
};
The top function works perfectly running locally but once on firebase it gives the error:
RequestError: Error: getaddrinfo EAI_AGAIN lh6.googleusercontent.com:443
at new RequestError (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:14:15)
at Request.plumbing.callback (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:87:29)
at Request.RP$callback [as _callback] (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/plumbing.js:46:31)
at self.callback (/user_code/node_modules/request/request.js:188:22)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at Request.onRequestError (/user_code/node_modules/request/request.js:884:8)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at TLSSocket.socketErrorListener (_http_client.js:310:9)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at connectErrorNT (net.js:1020:8)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
I am calling the function in the firebase auth trigger when a user is created as below:
exports.createUser = functions.auth.user().onCreate(event => {
if (event.data.photoURL) {
utils.downloadImageFromUrl(event.data.photoURL)
.then(function(res){
console.log("User Photo", res);
})
.catch(function(error){
console.log("Error", error);
})
}
});
Any help would be greatly appreciated.
Not entirely sure yet if this is the answer, but after reading the documentation, I read their free plan which says you cannot make any out bound requests. So I guess getting an image from a Url counts as an outbound request. After I start paying for their service, I will come back to verify if this was the problem.