Run javascript on rails server side - javascript

I have a rails app which is the REST api. Part of the models i have are javascript scripts that the users are able to upload.
I want to create a background worker task where i can make a network request and execute the javascript on the result of the network request server side and then save it to the database.
Now i have been told to consider using a Rails front end api and then use a NodeJS server for being the workers and executing javascript.
My main concern is developing a cross platform schema with nodejs orm and rails orm.
So my question is twofold is there a way of developing the database schema so it is in sync across both the Node JS server and the Rails server. Else is there a way of executing the javascript on the rails server?

You can use https://github.com/balderdashy/waterline or https://github.com/sequelize/sequelize or https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Shell to access same database as Rails app from Node.js app but make sure that you are only reading from database using Node.js not writing

Related

Connecting WebSockets from browser to other applications

Presentation
My application is split in 3 parts :
C# .Net 5.0 desktop application that harvest and distribute data
Angular 10 static application that control the C# app (start/Stop) and
print the data in the form of charts for the user
Azure Function Rest API used as a backend for the Angular Static
Application (.NET core 3.1)
My goal is to have a WebSocket used for "real time" communication between the C# and Angular app instead of relying on Http Request to Azure Function REST api
I don't use a "classic" server like express.js. Azure Function replaces it fully.
Because of that, I'm using the service Azure WebPubSub to host the sockets.
Problem : I cannot connect the websocket I create from Angular (Browser Application).
The problem doesn't come from WebPubSub because the WebSockets are fully working (sending & receiving) between my C# app, Azure Functions and even some node.js test scripts.
Also, my web socket created in the browser app can communicate with itself but no other apps get the message send from my websocket in browser app.
Question : Can I communicate from the browser to others app in real time (websocket based) with my current architecture and if yes, how ?
On the Azure WebPubSub Documentation Page there are no examples of what I try to achieve. Same thing searching on the internet.
A solution using an Express.js server linked to the app, being the could be used as the middleman between the browser app and c# app. But I want to avoid it if possible.
I just want to know if what I'm trying to do is possible.
Also, the app could be re-done in React.js so non-Angular specific answer are better.
The Web PubSub logstream sample:https://learn.microsoft.com/azure/azure-web-pubsub/tutorial-subprotocol?tabs=javascript sounds like a similar scenario.
Your AngualrJS Web App is similar to the logstream's web application, and it joins the stream group.
Your C# .Net 5.0 desktop application is similar to the logstream's message publisher application stream: https://learn.microsoft.com/azure/azure-web-pubsub/tutorial-subprotocol?tabs=csharp#publish-messages-from-client, which send messages to the stream group.

Getting data from backend and sending data to databse through node.js

I'm working on the front end of an application, the backend has been built in Spring Boot and MongoDB, I want to use a javascript library that provides chat functionality sockets.io and from examples I've seen, generally mongo is accessed via node and express. So what I was thinking was, getting usernames and such from the spring boot backend and then send data to my database (the messages) through node and express. Is something like this doable/recommended?

Does C++ respond to javascript generated requests in node.js?

Just started learning MongoDB for node.js
In the third module video, Overview of Building an App with MongoDB, the speaker says:
node.js is basically a C++ program that you control using V8 javascript. So any applications you write using node.js will be written in javascript and it will control this C++ application (node.js) and you'll be able to say something like they made request for this resource and your application actually in javascript will say: okey, they made a request for this resource, I don't have to respond to that. Now respond accordingly.
Does it mean, C++ is the one who responds to queries? But it's a language, not a server
Javascript is not the one who runs on the node's server?

How run Javascript on server side in Ruby on Rails

I intend to program a Bitcoin related Web application with Ruby on Rails.
Now there are some very useful libraries out there like Bitcoinjs or Bitcore that are written in Javascript and can be run on the client side in the Webbrowser, or server side with Node.js. However, since I intend to program this application with RoR, I wonder how I can take advantage of those libraries on the server side.
Is there a way (i.e. a gem) that allows interpreting Javascript on the server or can I start a Node.js instance in my RoR code by which I can then interpret those javascript libraries and execute their functions?
Any help is greatly appreciated.
Since it seams like you are very determined to use these nodejs libraries, here is how I would go about doing it. Since it is a very bad idea to run code in the wrong environment, I would instead make a setup like this.
Front end, ERB template => RoR server => nodeJs server
The idea behind this setup is that RoR is more like a task master and nodeJs is the worker. Your RoR app would figure out what needs to happen and then tell nodeJs to do it. Nodejs would then respond with the result which RoR would present back to the user. Here is an example:
Someone on the front end asks you app to transfer x bitcoins to someone else. Your Ruby on Rails app would then do whatever non bitcoin tasks were needed(ex: logging in, messaging people). Then your RoR app would send a request to the nodeJs server which would actually do the bitcoin tasks.
Here is how you would go about implimenting this setup.
Make the front end
Connect frontend actions with your RoR controllers
Make a RESTful HTTP API on your nodeJs.
If you don't already know what a RESTful API is, research it. It is a whole separate topic
Make an entry point for each action you want your app to perform.
Make this nodeJs server run localy, so people can not access it without going through your RoR app
Then use something like restful-client to use you nodeJs API from your RoR app

Node.js & MySQL - JavaScript application - how to interact with offline AND online databases

I am making a web-app using JavaScript. I plan to use Node.js to connect the app to an existing MySQL database.
First of all, will the Node code be written in the same .js file as my application? Or is it a separate file?
I need the data to be current at all times (even if you were to close the browser and re-open it, AND even in the event of the user not having a wifi connection), so my thought was to constantly update the local device's db and then to intermittently update the MySQL db. Is this the best strategy? If so, how exactly can Node talk to the offline db and MySQL?
First of all, will the Node code be written in the same .js file as my application? Or is it a separate file?
It is possible to keep your client side JavaScript in the same file as your server side JavaScript, but it doesn't make any sense to do so. They are separate programs. (Library files, on the other hand, are a different story).
so my thought was to constantly update the local device's db and then to intermittently update the MySQL db.
Working with a local database and syncing to a shared one is a common strategy. You do need to handle conflicting updates in a way that is sensible for your purposes though.
If so, how exactly can Node talk to the offline db and MySQL?
Node.js can't talk to the offline database, at least not directly.
You will have a web application running in the browser. It will use client side JavaScript with a client side database and some means of communicating with the server (often this is done by sending JSON over HTTP to and from a web service).
Then you will have a server side application running in Node.js. It will use server side JavaScript with a server side MySQL database and some means of communicating with the client (i.e. an HTTP server hosting a web service).

Categories

Resources