Load data using Node.js and PostGis - javascript

I have a huge file of data points, and I would like to load them. I am using Leaflet(Front-End)/Node.Js(Back-end)
I wanted to load the data that is inside borders.
I have two choices:
Using Node.Js
Using Leaflet (this is bad idea, 'cause the file is two huge to be
processed)
Now I want to load data whenever I move browser, The confusing part to me is, how can I load file every time from every move in front end
Move/Zoom Page (front End) ----> get Bounds --> On Node.JS Side (Back-end) --> Load data from PostGis by querying it using ORM --> write it on geojson file ---> plot data using on front-end part
main.js
1- Detect Movement / Zoom on
...
map.on('dragend', getPins);
map.on('zoomend', getPins);
...
function getPins(e){
bounds = map.getBounds();
var latMax = map.getBounds().getNorthEast().lat();
var lngMax = map.getBounds().getNorthEast().lng();
var latMin = map.getBounds().getSouthWest().lat();
var lngMin = map.getBounds().getSouthWest().lng();
var centerLATT = map.getCenter().lat();
var centerLON = map.getCenter().lng();
var centerZOOM = map.getZoom();
}
Node.Js / Express
var express = require('express');
var app = new express();
app.use(express.json());
app.use(express.static(__dirname + '/public'));
var port = Number(process.env.PORT || 5000);
app.listen(port);
2- Node module for returning PostGIS queries from this thread
I would like to update the point by loading from Postgis
var postgeo = require("postgeo");
postgeo.connect("postgres://user#host:port/database");
postgeo.query(
'SELECT gid,name,ST_X(the_geom) as longitude,ST_Y(the_geom) as latitude FROM GeoDataTable WHERE ST_Intersects( ST_MakeEnvelope('+latMax+", "+lngMax+", "+latMin+", "+lngMin" ', function(data) {
for(i=0; i<data.rows.length; i++){
var feature = new Feature();
feature.geometry = JSON.parse(result.rows[i].locale);
feature.properties = {"TTL", result.rows[i].ttl};
featureCollection.features.push(feature);
}
});
The problem is how can I pass latMax,lngMax,latMin,lngMin from front-end to backend and pass json file loaded from data base from back-end to front end automatically.

You can use socket.io to communicate with the node.js server in real time with the browser.
Your question is very generic, so it would take a lot of coding here to solve your problem, but basically you can use the socket.emit function from the socket.io library to send data from the browser to the server, and server back to the browser.

Related

WebTorrent creating a server through adding multiple torrents magnet's

I am trying to create a Node.js server that loads multiple torrent magnets and then serves a static directory to that .mp4 (endpoint), similar to what the demo is doing for a single torrent.
const WebTorrent = require('webtorrent')
var client = new WebTorrent()
var torrentId = '#'
const util = require('util')
client.add(torrentId, function (torrent) {
// Create HTTP server for this torrent
var server = torrent.createServer()
// console.log(util.inspect(torrent.createServer(), {showHidden: true, depth: null})) // To see what's going on
// Visit http://localhost:<port>/ to see a list of files
// Access individual files at http://localhost:<port>/<index> where index is the index
// in the torrent.files array
server.listen(8000) // s tart the server listening to a port
})
My end goal was to eventually have a database of magnet URL's, then have my server create a direct endpoint to each .mp4 file. This demo is working for the most basic re-creatable example for a single magnet, but I would like to load up multiple and serve the endpoints like:
client.add(magnet.forEach(), function(torrent) {
// Create server after multiple torrents loaded
})
I guess I really need to know how torrent.createServer() is able to make the static directory, or is there a way to load up multiple magnets?
Here is what it is creating for a single magnet url.
I know torrent.createServer() is making a simple HTTP server, I just do not understand also how it is indexing and serving the .mp4's directly without downloading them prior to the server.

Dynamically load different spotfire report from different Spotfire server

In our web application, we are integrating spotfire reports using "TIBCO Spotfire JavascriptAPI"
We need to display reports from multiple spotfire servers. for example we have one visualization report in "spotfire.cloud.tibco.com" and another report is in "spotfire-next.cloud.tibco.com"
In order to load report from these two servers, i need to include two SF servers like below
<script src="https://spotfire-next.cloud.tibco.com/spotfire/js-api/loader.js"></script>
<script src="https://spotfire.cloud.tibco.com/spotfire/wp/GetJavaScriptApi.ashx"></script>
so if i instantiate report using spotfire instance like "spotfire.webPlayer" which spotfire server will get trigger ?
var serverUrl = "https://spotfire-next.cloud.tibco.com/spotfire/wp/";
var analysisPath = "/Samples/Expense Analyzer Dashboard";
var customizationInfo = new spotfire.webPlayer.Customization();
var app = new spotfire.webPlayer.Application(serverUrl, customizationInfo,analysisPath);
var doc=app.openDocument("container");
var serverUrl2 = "https://spotfire.cloud.tibco.com/spotfire/wp/";
var analysisPath2 = "/Samples/Expense Analyzer Dashboard2";
var customizationInfo2 = new spotfire.webPlayer.Customization();
var app2 = new spotfire.webPlayer.Application(serverUrl2, customizationInfo2, analysisPath2);
var doc2=app2.openDocument("container2");
In order to load report from two different Spotfire server how can i achive this?
so if i instantiate report using spotfire instance like
"spotfire.webPlayer" which spotfire server will get trigger ?
Spotfire 7.6 and above uses Spotfire Server as a Load balance for Webplayers installed as Nodes. So based on Spotfire server request will go to Webplayer nodes managed by that server.
If you want to run your reports on specific Server/Webplayer Instances follow these steps:
create 2 resource pool Named: "Dashboard" and "Dashboard1"
assign some Webplayer instances from "server 1" to "Dashboard" pool
assign some Webplayer instances from "server 2" to "Dashboard1" pool
now you can assign reports to appropriate resource pool
I don't know if it would work but what if you staggered the scripts.
<script src="https://spotfire-next.cloud.tibco.com/spotfire/js-api/loader.js"></script>
<script>
var serverUrl = "https://spotfire-next.cloud.tibco.com/spotfire/wp/";
var analysisPath = "/Samples/Expense Analyzer Dashboard";
var customizationInfo = new spotfire.webPlayer.Customization();
var app = new spotfire.webPlayer.Application(serverUrl, customizationInfo,analysisPath);
var doc=app.openDocument("container");
</script>
<script src="https://spotfire.cloud.tibco.com/spotfire/wp/GetJavaScriptApi.ashx"></script>
<script>
var serverUrl2 = "https://spotfire.cloud.tibco.com/spotfire/wp/";
var analysisPath2 = "/Samples/Expense Analyzer Dashboard2";
var customizationInfo2 = new spotfire.webPlayer.Customization();
var app2 = new spotfire.webPlayer.Application(serverUrl2, customizationInfo2, analysisPath2);
var doc2=app2.openDocument("container2");
</script>
Could you download one of the loader.js and append a prefix manually and load that to your site and reference it.
You could build them in separate pages and then pull them in iframes if not overly complicated pages.
It's definitely a problem as JavaScript overrides same named functions with the last one.

Paper.js Server-Side Can't Work with Received Layers

In an effort to reduce clientside load, we are attempting to do the work of flattening Paper.js layers on a Node Express server. We have many layers to flatten with lots of image data. And rather than overwriting our data structure, we want to end up with new objects containing the rasterized (flattened) layers.
So we have an Express route that looks like this:
app.post('/flatten', function (request, response) {
var pdfs = JSON.parse(request.body.pdfs);
// Attempt to set up canvas on the server side to work with
var canvas = new paper.Canvas(1000, 1000);
paper.setup(canvas);
paper.view.draw();
for (var i = 0; i < pdfs.length; i++) {
var pdf = pdfs[i];
if (pdf !== null) {
for (var j = 0; j < pdf.pages.length; j++) {
if (pdf.pages[j].layer !== undefined) {
paper.project.layers.push(pdf.pages[j].layer); // Attempt to add to current project; necessary?
pdf.pages[j].layer.activate(); // Blows up
pdf.pages[j].layer.visible = true;
var layerAsRaster = pdf.pages[j].layer.rasterize(); // Blows up
layerAsRaster.visible = false;
var dataString = layerAsRaster.toDataURL();
pdfs[i].pages[j].pageImageData = dataString.split(',')[1];
pdf.pages[j].layer.visible = false;
}
}
}
}
response.send(pdfs);
});
The .layer is a native Paper.js layer that was made on the clientside.
We receive this error when hitting this route:
TypeError: pdf.pages[j].layer.activate is not a function
Thinking that perhaps we don't need to worry about activating layers on the serverside, I commented that out, but got the same error for the .rasterize line. (See the two lines commented "Blows up".)
Do I need to somehow import the layers we're receiving from the client into the project? I attempt to do that with the line:
paper.project.layers.push(pdf.pages[j].layer);
but to no avail.
How can I modify this method to successfully work with layers on the serverside?
The problem is that you are directly adding the layer to the project with the line paper.project.layers.push(pdf.pages[j].layer);
You're not allowed to directly manipulate paper's data structures. If you want to add a layer to a project use the following (note that this is not documented and will change with the next release of paper, but I don't think you'll need to do this):
(paperscript)
project.addChild(layer);
(javascript)
paper.project.addChild(layer);
It's not clear how pdf.pages[i].layer was created on the server side, whether it was imported via JSON (in which case it could already be inserted into the project), or whether it was removed from another project, so there may be other complications.
I think there is another problem. It doesn't appear that pdf.pages[i].layer has been turned into a server-side layer. So the key question is how was it transferred from the client to the server?
Here's a stab at the whole process:
(client side)
jsonLayer = paper.project.activeLayer.exportJSON();
// send jsonLayer to server using some method
(server side)
// get jsonLayer from client
layer = new paper.Layer();
layer.importJSON(jsonLayer);
layer should already be inserted into the project and should contain all the items that were in jsonLayer which was the layer on the client.
Here's a link to a discussion on how importJSON and exportJSON map to one another:
paperjs group discussion

Deconstruct/decode Websocket frames like Google Chrome does

I'm connecting to a website via websocket connection (client to server), I know how to encode the data and write it to the server (using the net module in node.js) but when I'm reading the data back I get odd characters in front of the important data, like if I'm suppose to get:
// Data needed on the left and data I'm receiving from websocket on the right
'inited\r\n' -> '�inited\r\n'
'n:2\r\n' -> '�n:2\r\n'
This is how I am getting the data from the server
Klass.prototype.connect = function(){
// this.port is equal to 8080 and the exact server varys, but it's not that important anyways since the problem is decoding the data properly.
var that = this;
var buffer = "";
this.socket = new net.createConnection(this.port, this.server);
this.socket
.on("connect", function(){
that.sendHandshake(); // just sends a standard client to server handshake
})
.on("data", function(recv){
// .split('\r\n\r\n').join('\r\n') needed to separate the server handshake from the data I am trying to parse
buffer += recv.toString('utf-8').split('\r\n\r\n').join('\r\n');
while (buffer){
var offset = buffer.indexOf('\r\n');
if (offset < 0)
return;
var msg = buffer.slice(0, offset);
// parseMsg(msg)
buffer = buffer.slice(offset + 3);
}
});
};
I am probably doing a lot of things improperly in the code above, but I'm not quite sure how to do it exactly so that is the best I got for now.
Problem is I don't know how to remove the mystery/special characters. Sometimes there is only 1 mystery/special character, but other times there is multiple ones depending on the data but they are never after the important data I need to check.
When I use Google Chrome and view the data on through tools->JavaScript console->network tab and find the websocket stream I'm looking for Google parses it correctly. I know it's possible since Google Chrome shows the correct frames, how do I deconstruct/decode the data so I can view the correct frames on the terminal?
I don't really need it in a particular language as long as it works I should be able to port it, but I would prefer examples/answers in node.js since that is the programming language I am using to connect to the server.

Node js / MongoDB replica set array in javascript

Warning: I'm a novice programmer (more of sysadmin). We were given an node js application that's using MongoDB. From what I can tell, the mongo.js file is using mongojs and monq java classes. It was setup with only one MongoDB and I'm trying to setup a new HA environment to use a replica set. Here is what they provided:
var mongojs = require('mongojs');
var monq = require('monq');
var dbName = 'exampledb';
var db = mongojs(dbName, ['collections']);
var client = monq('mongodb://127.0.0.1/exampledb', { w: 1 });
exports.db = db;
exports.ObjectId = mongojs.ObjectId;
exports.monqClient = client;
Now for a replica set, according to this article, I need to make the following change:
var db = mongojs('replset0.com, replset1.com, replset2.com/mydb?slaveOK=true?', ['collections']);
I'm not entirely sure what I need to do for the line after that. I'm guessing I would have to create an array that would contain the host name and port # for each member of the replica set (setup is primary, secondary, arbiter) such as:
var replSet = new replSet();
var replSet[0] = "server0:port0"
var replSet[1] = "server1.:port1"
var replSet[2] = "server2.:port2"
How would I go about detecting which node is the primary? Also if the primary were to fail, I would have to restart the node js application (using forever)?
I found the answer as it's calling MongoDB's URI
http://docs.mongodb.org/manual/reference/connection-string/
Should be something like:
var client = monq('mongodb://server0:port0,server1:port1,server2:port2/[dbname]?replicaSet=[replicaSet Name]
First question: As long as you give it all of the members in the connection string, the mongojs driver should be able to figure out which one is primary. No need to figure it out yourself.
Second question: No, you don't have to restart the node app. The other members in the set will elect a new primary, although it takes time for mongo to detect failure and run the election.
For more information, see the mongodb docs on replica sets.

Categories

Resources