Socket.io disconnecting - angularjs

I have a chat done in nodejs, Express, socket.io and angular. It works well but disconnects sometimes and at random times. Generally the connection lasts no more than 2 minutes. I get several net :: ERR_CONNECTION_TIMED_OUT on the console.
PS.: I'm using apache 2.2 on CentOS with certified ssl.
Any tips?
My server.js header is below
#!/bin/env node
var express = require('express'),
path = require('path'),
app = express(),
logger = require('morgan'),
_m = require("./models/Message"),
Message = _m.m,
NewMessage = _m.n,
Group = _m.g,
Online = _m.o,
DeletedMessage = _m.d,
LastMessage = _m.l,
_mTASK = require("./models/Task"),
_Task = _mTASK.t,
TaskComment = _mTASK.c,
TaskLog = _mTASK.l,
TaskModel = _mTASK.m,
TaskNotification = _mTASK.n,
_d = require("./lib/Connection");
app.use(logger('dev'));
app.set('port', 3000);
app.set('ipaddr', "127.0.0.1");
var server = require('http').createServer(app);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
server.listen(app.get('port'), app.get('ipaddr'), function() {
console.log('Express server listening on IP: ' + app.get('ipaddr') + ' and port ' + app.get('port'));
});
var io = require("socket.io")(server);
io.set("origins", 'https://meusite.me:*');
io.set('transports', ['polling', 'websocket']);

A bit of socket.io background
Server sends a heartbeat to the client every X seconds where X == the
heartbeat interval configuration value.
If the client fails to respond, socket considers the connect dead
Client waits for a heartbeat from the server every N seconds where N == the heartbeat timeout configuration value.
Both of these values are set on the server with heartbeat timeout being sent to the client when an individual connection is opened.
Given the file above, you could set the heartbeat timeout with something like ...
//WARNING io.set() has been depricated
var io = require("socket.io")(server);
io.set('heartbeat interval', '30000');
io.set('heartbeat timeout', '45000');
io.set("origins", 'https://meusite.me:*');
io.set('transports', ['polling', 'websocket']);
//Setting your server configuration is now done via ..
var socket = require('socket.io')({
// options go here
'configOption': 'configValue';
});

Related

Lagom Framework Topic Websocket Timeout connection closed with error

I have created a lagom Topic in my service.
def addingsTopic(): Topic[ProcessDefinitionAdded]
the descriptor:
override def descriptor: Descriptor = {
import Service._
named(ProductionService.TOPIC_NAME).withCalls(
pathCall(pathProcessDef(":id"), getProcessDefinition _)
).withTopics(
topic("processdef", addingsTopic _ )
.addProperty(
KafkaProperties.partitionKeyStrategy,
PartitionKeyStrategy[ProcessDefinitionAdded](_.entityId.toString)
),
topic("productionserviceorder", ordersTopic _)
.addProperty(
KafkaProperties.partitionKeyStrategy,
PartitionKeyStrategy[OrderAddedTopic](_.entityId.toString)
)
).withAutoAcl(true)
}
Until here all is clear. Now I have a play webpage. I have in my controller a Websocket.
val k = productionService.addingsTopic().subscribe.atMostOnceSource.mapAsync(1){e ⇒
productionService.getProcessDefinition(e.entityId).invoke().map{e ⇒
Json.toJson(e)(ProcessDefinitionResponse.format)
}
}.toMat(BroadcastHub.sink(256) )(Keep.right).run().recover{
case e ⇒ println(e)
Json.toJson("error "+e)
}
val f = Flow.fromSinkAndSource(Sink.ignore,k.concat(Source.maybe))
def fetchProcessDefinition() = WebSocket.accept[String,JsValue] { req ⇒
f
}
My javascript website use this to connect:
<script>
try{
console.log("START");
var socket = new WebSocket("ws://localhost:9000/processdef/stream");
socket.onopen = function(s){
console.log("OPEN");
console.log(s);
};
socket.onerror = function(error){
console.log("ERROR: ");
console.log(error);
};
socket.onmessage = function(msg){
console.log("Message:");
console.log(JSON.stringify(msg.data));
};
socket.onclose = function(closeevent){
console.log("CLOSE:");
console.log(closeevent);
};
socket.send("")
}catch (e) {
console.error(e);
}
</script>
I load the page on localhost after sbt runAll. It takes a time then the websockets connects (sometimes a few minutes and sometimes a few seconds). After ~ 90 seconds the console prints that the connection is closed out.
On IDE I get this error:
2018-10-18T10:54:16.655Z [error] akka.actor.ActorSystemImpl [sourceThread=application-akka.actor.default-dispatcher-213, akkaSource=akka.actor.ActorSystemImpl(application), sourceActorSystem=application, akkaTimestamp=10:54:16.654UTC] - Websocket handler failed with The connection closed with error: The connection was reset by peer
akka.stream.StreamTcpException: The connection closed with error: The connection was reset by peer
2018-10-18T10:54:16.674Z [error] akka.actor.ActorSystemImpl [sourceThread=application-akka.actor.default-dispatcher-231, akkaSource=akka.actor.ActorSystemImpl(application), sourceActorSystem=application, akkaTimestamp=10:54:16.674UTC] - Websocket handler failed with The connection closed with error: The connection was reset by peer
akka.stream.StreamTcpException: The connection closed with error: The connection was reset by peer
That is very stressful. Do I have any mistakes in my code?
Thanks for any help.

Issue Creating New MSSQL Record in Node/Angular App

I am trying to create a new row in an existing Azure MSSQL database through my node/angular app. The GET request fires correctly, and the form I am using to generate the data generates the JSON correctly from what I can tell, but when the POST function fires, I get the following error:
Trace: { RequestError: JSON text is not properly formatted. Unexpected character 'o' is found at position 1.
at RequestError (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\tedious\lib\errors.js:34:12)
at Parser.<anonymous> (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\tedious\lib\connection.js:614:36)
at Parser.emit (events.js:182:13)
at Parser.<anonymous> (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\tedious\lib\token\token-stream-parser.js:54:15)
at Parser.emit (events.js:182:13)
at addChunk (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\readable-stream\lib\_stream_readable.js:291:12)
at readableAddChunk (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\readable-stream\lib\_stream_readable.js:278:11)
at Parser.Readable.push (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\readable-stream\lib\_stream_readable.js:245:10)
at Parser.Transform.push (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\readable-stream\lib\_stream_transform.js:148:32)
at doneParsing (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\tedious\lib\token\stream-parser.js:110:18)
message:
'JSON text is not properly formatted. Unexpected character \'o\' is found at position 1.',
code: 'EREQUEST',
number: 13609,
state: 4,
class: 16,
serverName: 'xxxxxxx',
procName: 'createReport',
lineNumber: 5 }
at Object.fnOnError (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\express4-tedious\index.js:104:25)
at Request.userCallback (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\express4-tedious\index.js:59:64)
at Request._this.callback (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\tedious\lib\request.js:60:27)
at Connection.endOfMessageMarkerReceived (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\tedious\lib\connection.js:1922:20)
at Connection.dispatchEvent (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\tedious\lib\connection.js:1004:38)
at Parser.<anonymous> (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\tedious\lib\connection.js:805:18)
at Parser.emit (events.js:182:13)
at Parser.<anonymous> (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\tedious\lib\token\token-stream-parser.js:54:15)
at Parser.emit (events.js:182:13)
at addChunk (C:\Users\jlea\Desktop\Code\tnplan-boot\node_modules\readable-stream\lib\_stream_readable.js:291:12)
events.js:167
throw er; // Unhandled 'error' event
^
Here is my app.js code:
const express = require('express');
const config = require('config');
const bodyParser = require('body-parser');
const tediousExpress = require('express4-tedious');
const cors = require('cors');
const path = require('path');
const app = express();
app.use(function (req, res, next) {
req.sql = tediousExpress(config.get('connection'));
next();
});
const corsOptions = {
origin: '*',
optionsSuccessStatus: 200
};
app.use(express.static(__dirname + '/dist/tnplan-boot'));
app.use(bodyParser.json());
app.options('*', cors(corsOptions));
app.use('/monthlyReport', require('./routes/monthlyReport'));
// "index" route, which serves the Angular app
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '/dist/tnplan-boot/index.html'));
});
// catch 404 and forward to error handler
app.use(function (req, res, next) {
const err = new Error('Not Found: ' + req.method + ":" + req.originalUrl);
err.status = 404;
next(err);
});
app.set('port', process.env.PORT || 8080);
const server = app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + server.address().port);
});
module.exports = app;
and my sql routes code:
const router = require('express').Router();
const TYPES = require('tedious').TYPES;
/* GET reports. */
router.get('/', function (req, res) {
req.sql("select * from jacksonwaste for json path")
.into(res, '[]');
});
/* GET single report. */
router.get('/:id', function (req, res) {
req.sql("select * from jacksonwaste where id = #id for json path, without_array_wrapper")
.param('id', req.params.id, TYPES.Int)
.into(res, '{}');
});
/* POST create report. */
router.post('/', function (req, res) {
req.sql("exec createReport #report")
.param('report', req.body, TYPES.NvarChar)
.exec(res)
console.log(req.body);
});
/* PUT update report. */
router.put('/:id', function (req, res) {
req.sql("exec updateReport #id, #report")
.param('id', req.params.id, TYPES.Int)
.param('report', req.body, TYPES.NvarChar)
.exec(res);
});
/* DELETE single report. */
router.delete('/:id', function (req, res) {
req.sql("delete from jacksonwaste where id = #id")
.param('id', req.params.id, TYPES.Int)
.exec(res);
});
module.exports = router;
and finally, my mssql stored procedure code:
/****** Object: StoredProcedure [dbo].[createReport] Script Date: 8/15/2018 9:13:31 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[createReport](#report nvarchar(max))
as begin
SET NOCOUNT ON
insert into jacksonwaste (reportuser, reportdate, percentcomp, err, errdate, acquisition, acqdate, pns, pnsdate, bidtabs, bidtabsdate, constructionstart, constartdate, constructionend, conenddate, monitored, monitorready,
contractoractivity, lastcafsubmission, mostrecentinvoice, phases, phaseupdate, hasoccured, willoccur, issues, tnecdhelp)
select *
from OPENJSON(#report)
WITH (
reportuser nvarchar(128),
reportdate date,
percentcomp int,
err bit,
errdate date,
acquisition bit,
acqdate date,
pns bit,
pnsdate date,
bidtabs bit,
bidtabsdate date,
constructionstart bit,
constartdate date,
constructionend bit,
conenddate date,
monitored bit,
monitorready bit,
contractoractivity bit,
lastcafsubmission date,
mostrecentinvoice date,
phases bit,
phaseupdate nvarchar(1000),
hasoccured nvarchar(1000),
willoccur nvarchar(1000),
issues nvarchar(1000),
tnecdhelp nvarchar(1000)
)
end
EDIT: added the JSON object that is being delivered to the sql request to show that there's no unexpected character that I can see:
{ reportuser: 'john',
reportdate: '2018-08-11',
percentcomp: '4',
err: true,
pns: true,
pnsdate: '2018-08-04',
errdate: '2018-08-16',
constructionstart: true,
constartdate: '2018-08-29',
lastcafsubmission: '2018-08-23',
mostrecentinvoice: '2018-08-24',
phaseupdate: 'test',
hasoccured: 'test',
willoccur: 'test',
issues: 'test',
tnecdhelp: 'test' }
Where am I going wrong?
For the record, in hopes this helps someone in the future:
I recently had a very similar issue (Exact same RequestError) and found out that I was passing the JSON as an object, whereas express4-tedious was expecting a string. JSON.stringify(req.body) did the trick.
In other words, changing the equivalent of your
req.sql("exec updateReport #id, #report")
.param('id', req.params.id, TYPES.Int)
.param('report', req.body, TYPES.NvarChar)
.exec(res);
to
req.sql("exec updateReport #id, #report")
.param('id', req.params.id, TYPES.Int)
.param('report', JSON.stringify(req.body), TYPES.NvarChar)
.exec(res);
solved my problem.

Node promises and catch issue

I am currently writing a database class that receives a node http request and saves data into a db.
It uses MSSQL (yeah...), therefore I'm using node-mssql.
I created a class to manage DB access, code:
"use strict";
var config = require('../../config/mainConfigs');
const mssql = require('mssql');
class db {
constructor(){
this._pool = null;
}
get_pool(){
if (!this._pool) {
if (config.Logging.DB.type == 'mssql'){
const dbOptions = {
user: config.Logging.DB.user,
password: config.Logging.DB.password,
server: config.Logging.DB.mssql.server,
database: config.Logging.DB.mssql.database,
options: {
encrypt: config.Logging.DB.encrypt
}
};
this._pool = new mssql.ConnectionPool(dbOptions);
}
}
return this._pool;
}
insertHTTPRequest(req){
const pool = this.get_pool();
if (config.Logging.DB.type == 'mssql'){
if (pool._connected){
var request = new mssql.Request(pool);
var query = `INSERT INTO SD_LOG (
MODULE,
INSTANCE,
REMOTE_ADDR,
USERNAME,
USER_AGENT,
HTTP_METHOD,
HTTP_REQ_URL
) OUTPUT Inserted.ID_SD_LOG VALUES (
#module,
#instance,
#remote_addr,
#username,
#user_agent,
#http_method,
#http_req_url
)`;
request.input('module', 'tokenizer');
request.input('instance', config.Deployment.instance);
request.input('remote_addr', req.ip);
request.input('username', req.user.displayName);
request.input('user_agent', req.headers['user-agent']);
request.input('http_method', req.method);
request.input('http_req_url', req.url);
return request.query(query);
}else{
return pool.connect().then(() => {
var request = new mssql.Request(pool);
var query = `INSERT INTO SD_LOG (
MODULE,
INSTANCE,
REMOTE_ADDR,
USERNAME,
USER_AGENT,
HTTP_METHOD,
HTTP_REQ_URL
) OUTPUT Inserted.ID_SD_LOG VALUES (
#module,
#instance,
#remote_addr,
#username,
#user_agent,
#http_method,
#http_req_url
)`;
request.input('module', 'tokenizer');
request.input('instance', config.Deployment.instance);
request.input('remote_addr', req.ip);
request.input('username', req.user.displayName);
request.input('user_agent', req.headers['user-agent']);
request.input('http_method', req.method);
request.input('http_req_url', req.url);
request.query(query);
}
}
}
}
I am using a middleware in the routes to save the request into the DB, like this:
app.use(function(req, res, next){
//send req to DB, get ID_SD_LOG from DB, assign to req.id
db_instance.insertHTTPRequest(req).then((genid)=>{
req.id = genid;
next();
}).catch((err)=>{
combinedLogger.error(err);
res.send('Database is unavailable.');
res.end();
})
});
Thing is, I am getting an error, however, in console, the error is undefined. So I really can't figure out whats wrong. I can imagine it's the connect() method, as I tested logging in console static string in the db request and I don't them, which I assume it's not even accessing that part.
Any help?
Thanks!

Prerender.io throws socket error

I have been trying to make prerender.io working for a week now, I tried everything but nothing worked.
I have a node server:
'use strict';
var path = require('path');
var _ = require('lodash');
var express = require('express');
var app = module.exports = express();
var prerender = require('prerender-node')
.set('prerenderToken', 'My Key');
// These search engine bot do not adheres to google's _escaped_fragment_
// proposal, so we use user agent to detect them.
var moreCrawlerUserAgents = [
'Slurp!',
'MSNBot',
'YoudaoBot',
'JikeSpider',
'Sosospider',
'360Spider',
'Sogou web spider',
'Sogou inst spider',
'baiduspider',
'facebookexternalhit',
'twitterbot',
'rogerbot',
'linkedinbot',
'embedly',
'quora link preview',
'showyoubot',
'outbrain',
'pinterest',
'developers.google.com/+/web/snippet',
'slackbot',
'vkShare',
'W3C_Validator',
'redditbot'
];
prerender.set('crawlerUserAgents', _.union(
prerender.crawlerUserAgents, moreCrawlerUserAgents));
app.use(prerender);
var options = {
maxAge: '60d',
setHeaders: function(res, path, stat) {
// Webfonts need to have CORS * set in order to work.
if (path.match(/ttf|woff|woff2|eot|svg/ig)) {
res.set('Access-Control-Allow-Origin', '*');
}
}
};
var dist_path = '/client/dist/';
app.use(express.static(path.join(__dirname, dist_path), options));
app.use(function(req, res) {
res.sendFile(path.join(__dirname + dist_path + '/index.html'));
});
var port = process.env.PORT || 8000;
app.listen(port, '0.0.0.0');
console.log("Listening on port " + port);
It works fine until I pass _escaped_fragment_= as a query string parameter then it throws these two errors:
Error: getaddrinfo EMFILE
at Object.exports._errnoException (util.js:746:11)
at errnoException (dns.js:49:15)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:94:26)
OR
Error: socket hang up
at createHangUpError (_http_client.js:215:15)
at Socket.socketOnEnd (_http_client.js:300:23)
at Socket.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)
I don't know what the problem is, please help!
Thanks.

I am trying to connect to postgres database instance in aws but I can't

I have the following code.
var express = require('express');
var app = express();
var path = require('path');
var pg = require('pg');
var conString = "postgres://user:password#endpoint:5432/StudentRecords";
//this initializes a connection pool
//it will keep idle connections open for a (configurable) 30 seconds
//and set a limit of 20 (also configurable)
var client = new pg.Client(conString);
client.connect(function(err) {
if(err) {
return console.error('could not connect to postgres', err);
}
else{
console.log("asdfaf")
}
});
client.connect();
It shows cannot connect to postgres. But when I try connecting from the terminal using
psql --host=endpoint --port=5432 --username xxxxx --password --dbname=StudentRecords
I can connect. Why is the code not working? Error I am getting is:
could not connect to postgres [Error: Connection terminated]

Resources