My Problem:
My goal is to send weekly mails and for a test I made with the modules node-schedule and nodemailer a website which should send me every minute an email.
My issue:
when I'm running it on my local machine (Windows OS) on the localhost everything works, but as long as I'm uploading it on the server I'm getting:
[enter image description here][1]
[1]: https://i.stack.imgur.com/Ett2l.png
On english:
The website is not reachable
g.ch has refused connection.
Try the following:
Check connection
Check proxy and firewall
ERR_CONNECTION_REFUSED
The server has the same node js version as the client. The Server is a linux Debian server version 10.4 and the computer I'm programing on has the windows 10 OS.
I've tried to add the following thing to my server.js:
const nodemailer = require('nodemailer');
const schedule = require('node-schedule');
var transporter = nodemailer.createTransport({
host: "chestonag-ch.mail.protection.outlook.com", // hostname
secure: false, // use SSL
port: 25, // port for secure SMTP
tls: {
rejectUnauthorized: false
}
});
var mailOptions = {
from: 'no-reply-mailer#company.ch', // sender address
to: 'name#company.ch', // list of receivers
cc: '', // Comma separated list or an array
subject: 'test upgrde nodemailer subject', // Subject line
html: '<b>Hello world </b>' // html body
};
const job = schedule.scheduleJob('30 * * * * *', function() {
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log("/sendmail error");
console.log(error);
res.sendStatus(500);
return;
} else {
console.log("Message sent: " + info.response);
socketTimeout: 30 * 1000 // 0.5 min: Time of inactivity until the connection is closed
transporter.close(); // shut down the connection pool, no more messages
res.sendStatus(200);
}
transporter.close(); // shut down the connection pool, no more messages
});
});
and the same thing to one of my .js files in the routes.
That you know: The project is with vue.
I have a client side and a server side and my changes were only on the server side.
When I run it local it worked and on the server it didn't and the logs were also empty.
if anyone has an idea how to fix that or an alternative solution, please let me know.
If it's good to know: my goal at the end is to make an emailing system, where you can subscribe to channels and unsubscribe them to receive weekly mails.
Related
I am using RabbitMq as a Queueing Mechanism for my message handling.
So far, all the queues are generating fine on the localhost but when I moved on to the server. It started showing
connection timeout error
This is my connecion string.
var amqp = require('amqp');
var connection = amqp.createConnection({
url: 'amqp://username:passwprd#server-IP:5672/'
})
connection.on('error',(err)=>{
console.log(err);
});
var options = { autoDelete:false,
durable:false,
expiration:'20000',
};
connection.on('ready',()=>{
connection.queue('queueName',options,(queue)=>{
queue.bind('#');
queue.subscribe({ack:true},message =>{
console.log(message);
});
});
});
Can anyone tell me what i am doing wrong here
I have launched the Ubuntu EC2 instance and using putty utility I have installed rabbitq on the same.
To know the status of rabbitmq service, you can type service rabbitmq-server status
You can refer documentation from Rabbitmq node Rabbitmq
I have tried with below code on aws instance and its working
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
console.log(conn);
});
Output
Actually I get what you are asking
Just type this code
var connection = amqp.createConnection({
url: 'amqp://localhost'
})
on your server side code and the project will run fine as it is running on my server.
You don't need to specify the username and password or your server-ip.
I am using nodemailer (v1.0.4) to send emails in my Node application. Here is the code:
smtpUtil.js
var nodemailer = require("nodemailer");
var config = require("../config").mailgun;
var transporter = nodemailer.createTransport({
service: 'Mailgun',
auth: {
user: config.username,
pass: config.password
},
});
transporter.mailSent = function(mailOptions, callback) {
transporter.sendMail(mailOptions, function(error, response) {
if (error) {
console.log("Error in sending mail", error);
callback(new Error(error));
} else {
console.log("Email sent successfully");
callback();
}
});
}
module.exports = transporter;
I just include this smtpUtil.js in other files and then call transporter.mailSent(mailOpts, callbackFn); to send the email.
My question is: how to add max timeout time & gracefully handle those in the process?
The reason I am asking about above configuration is that recently in code, I invoked transporter.mailSent(mailOpts, callbackFn). But due to some reason (possibly infinite timeout), the callbackFn was never triggered (neither success nor failure).
Latest NodeMailer module though gives 3 timeout options, as follows:
options.connectionTimeout how many milliseconds to wait for the
connection to establish
options.greetingTimeout how many milliseconds to wait for the greeting after connection is established
options.socketTimeout how many milliseconds of inactivity to allow
But I am not sure which one would be relevant here to tackle the callbackFn never getting triggered issue.
I solved this issue by adding a max TTL to process the email sending part.
Since I was using kue, it was rather easy to configure a max TTL.
I tried client 1 and client 2 program I can able to easily communicate with them. I can easily send the messages and receive the messages with them, but I don't know if one client is disconnected, how can I send the disconnected message to subscribed clients.
client 1:
var mqtt=require("mqtt");
var express=require("express");
var app=express();
var options={
keepalive:100,
port: 1883,
clientId:'1',
clientSession:false,
host: "http://localhost:8000",
will:
{
topic:'willMag',
payload:"connection closed abnormallly r",
qos:1,
retain:true
}
};
var client=mqtt.connect("tcp://192.168.43.137:1883",options);
client.on("connect",function()
{
setInterval(function()
{
client.publish("ranjith/princy","hello love you princy",function()
{
console.log("message published in client1");
});
},2000);
client.subscribe("bv/nivi");
client.on("message",function(topic,message)
{
console.log("I recieved the topic:"+topic);
console.log("I recieved the message:"+message);
});
});
client.on("disconnect",function()
{
console.log("disconnected client1");
});
app.listen(8000,function()
{
console.log("server listen at port 8000");
});
client 2:
var mqtt=require("mqtt");
var express=require("express");
var app=express();
var options={
keepalive:100,
port: 1883,
clientId:'2',
clientSession:false,
host: "http://localhost:8086",
will:
{
topic:'willMag',
payload:"connection closed abnormallly b",
qos:1,
retain:true
}
};
var client=mqtt.connect("tcp://192.168.43.137:1883",options);
client.on("connect",function()
{
setInterval(function(){
client.publish("bv/nivi","hello love you nivi",function()
{
console.log("message published in client2");
});
},2000);
client.subscribe("ranjith/princy");
client.on("message",function(topic,message)
{
console.log("I recieved the topic:"+topic);
console.log("I recieved the message:"+message);
});
});
client.on("disconnect",function()
{
console.log("disconnected client2");
});
app.listen(8086,function()
{
console.log("server listen at port 8000");
});
It's not totally clear what you are asking here, but:
With MQTT you can not know what clients are subscribed to what topics
There is no way to know if a message has been delivered to a specific client
You can build a system to determine if a client is probably online. You need to make use of the Last Will and Testament (LWT) feature.
When your client connects it publishes a retained message to a given topic (e.g. client1/online payload: 1)
You set the LWT to publish payload 0 to the same topic if the client goes off line due to a crash/network failure
When you shut the client down cleanly you need to publish a 0 to the topic manually as the LWT will only fire if there is a failure.
I have a problem with socket.io/node.js. My Client cannot receive Messages from Server after switch Server with "forceNew true"
My Script on Client-side looks like this:
var socket;
function connect_server(host){
socket = new io.connect('//'+host+':843', {
'forceNew': true,
'transports': ['websocket']
});
}
connect_server('server1.domain.com'); // First time connect Server1
function disconnect(){
socket.emit('forceDisconnect');
}
socket.on('messages', function (data) {
console.log('Message from Server: '+data);
});
<div onclick="disconnect(); connect_server('server2.domain.com');">Connect to Server2</div>
<div onclick="socket.emit('emit_server', 'Test'); ">Send Message</div>
My Client connect to "server1.domain.com" and can send Messages to "server1.domain.com" via "socket.emit". Client can receive Messages from Server1. It runs perfect. No problems.
Problem: When my Client connect to "server2.domain.com" he can emit Messages to Server2, but Server2 can not emit Messages to Client. Client dont receive any Messages from Server2.
Does anyone know a solution?
on client-side i use "socket.io-1.4.5.js", on server-side nodejs v4.2.6 + Option "io.origins(':');"
This Solution works. "socket.on"-Listener must be renew inside connect_server-function.
function connect_server(host){
socket = new io.connect('//'+host+':843', {
'forceNew': true,
'transports': ['websocket']
});
socket.on('messages', function (data) {
console.log('Message from Server: '+data);
});
}
I make the first steps in the node js and xmpp
I need to run at xmpp server on node js for messaging
Here's the process:
I use node-xmpp server https://github.com/astro/node-xmpp
run the example of a server (/examples/c2s.js)
join to server with two clients (clients tested on other servers jabber - it works and the messages are sending through)
Clients have authorization on my server.
But when I send a message from one client to another, the message comes to the server (I see it in the logs)
and that was the message does not come to recipient
I don `t know where to look for the problem
server configuration ? routing ? messaging may be necessary to add yourself ?
help me plz
my server code (by examples)
var xmpp = require('../lib/node-xmpp');
var c2s = new xmpp.C2SServer({
port: 5222,
domain: 'localhost'
});
// On Connect event. When a client connects.
c2s.on("connect", function(client) {
c2s.on("register", function(opts, cb) {
console.log("REGISTER");
cb(true);
});
client.on("authenticate", function(opts, cb) {
console.log("AUTH" + opts.jid + " -> " +opts.password);
cb(null);
});
client.on("online", function() {
console.log("ONLINE");
client.send(new xmpp.Message({ type: 'chat' }).c('body').t("Hello there, little client."));
});
client.on("stanza", function(stanza) {
console.log("STANZA" + stanza);
});
client.on("disconnect", function(client) {
console.log("DISCONNECT");
});
});
I run a server and connect to it by this code
var xmpp = require('../lib/node-xmpp');
var argv = process.argv;
if (argv.length < 6) {
console.error('Usage: node send_message.js <my-jid> <my-password> <my-text> <jid1> [jid2] ... [jidN]');
process.exit(1);
}
var cl = new xmpp.Client({ jid: argv[2], password: argv[3] });
cl.addListener('online',
function() {argv.slice(5).forEach(
function(to) {cl.send(new xmpp.Element('message', { to: to,type: 'chat'}).c('body').t(argv[4]));
});
// nodejs has nothing left to do and will exit
// cl.end();
});
cl.addListener('stanza',
function(stanza) {
if (stanza.is('message') &&
// Important: never reply to errors!
stanza.attrs.type !== 'error') {
console.log("New message");
// Swap addresses...
stanza.attrs.to = stanza.attrs.from;
delete stanza.attrs.from;
// and send back.
cl.send(stanza);
}
});
cl.addListener('error',
function(e) {
console.error(e);
process.exit(1);
});
The short answer: change cb(null) to cb(null, opts).
The long answer:
client.on("authenticate", function(opts, cb) {...}) registers what the server will do when the client tries to authenticate itself. Inside node-xmpp, it will look for the authentication mechanism first and the mechanism will then call the callback and retrieve the authentication results via cb.
By default, the Plain authentication is used. You can check out how it works here: https://github.com/node-xmpp/node-xmpp-server/blob/master/lib/authentication/plain.js. With Plain the opts stores the jid and password.
Then to inform node-xmpp that authentication failed/sucessed, we need to look into mechanism, https://github.com/node-xmpp/node-xmpp-server/blob/master/lib/authentication/mechanism.js, inherited by Plain.
this.authenticate(this.extractSasl(auth), function (err, user) {
if (!err && user) {
self.success(user)
} else {
self.failure(err)
}
})
Here, cb requires two parameters. When err is null and user is non-null, it indicates authentication successes. Otherwise, failed.
I am no expert on neither node.js nor xmpp. But reading your code. I assume the "stanza" is the event where a client sent a message. You asked it to log the message, but you gave no instructions on how to route it to the recipient. You should break down the received message on the server into message body and recipient, and ask your server to send it to the recipient.
Alex you have used C2SServer which connects a stream between a server and a client. When you send a message from one client to another they get to server. Now its responsibility of the sever to relay them back to actual receiver.
One possible solution is to keep client object is a global object corresponding to their jids when client is authenticated, when you get a message for that client you can get that from global variable and route the message to actual client kept in global variable.
You can get the text message & receiver JID from server. Just break the stanza in following ways and put this before error listeners:-
cl.on('stanza', function(stanza) {
if (stanza.is('message') && (stanza.attrs.type !== 'error')) {
var body = stanza.getChild('body');
if (!body) {
return;
}
console.log(stanza.attrs.from+" Says: "+body.children[0]);
}
});
In "authenticate", may an argument not be enough for a callback?
NG:
cb(null);
OK:
cb(null, opts);