Passing data to ruby script when executed through JavaScript - javascript

So I was learning JavaScript/jQuery and building a website as I go along. Now I need to have a database, and my friend recomended learning Ruby and using it to handle data. From this question: JavaScript Execute Ruby Script It shows how I could execute the Ruby script, but I was wondering if it is possible to send data to that script so it can push it to a MySQL database?
Basically the user would submit a string, and all the stuff is dynamically generated by JS, so I want to send that string to the SQL db as soon as the user submits it. If anyone can point me in the right direction in terms of readings. I don't have much knowledge/experience in Ruby so anything that redirects me to something useful for this particular task would be great. Thanks!

The question you referred to is probably not what you want. That will only work if the server is on the same machine as your browser and if that machine runs Windows.
For a website, that everybody can visit you need to have a server that runs some software - in your case written in ruby - and that is sent requests by the browser - in your case through your JavaScript program.
To do that you need to send an XMLHttpRequest. For jQuery you can read about this in the docs or in a tutorial. This way you get your browser to talk to the server.
For the server to listen and respond you should use some framework like Ruby on Rails or (not Ruby but Python) Django.

Related

Safe Javascript/Sql connection

im trying to create a simple website with HTML/CSS and Javascript. Basically the user should be able to input a number into a textfield and "send it" with a button. When the button got pressed i want to run a Javascript function that searches the number in a sql database.
Creating all that stuff shouldnt be a big problem for me, but i have no clue how to create a safe connection between JS and SQL. I have read that a direct connection with javascript is very insecure.
Some people recommend to use java or c# to built an sql connection. How would that work? Basically just an Javascript code, that runs an java/c# application(which builds an sql connection) and returns the needed sql data?
Also heard that its possible to create a sql connection with node.js, is this safe? Or is another method more suitable?
Greetings
I have read that a direct connection with javascript is very insecure
The danger is in giving direct access to your database to the client. JavaScript is most commonly run client-side in web browsers, so for it to access the database you would have to give the browser (and thus the visitor) a username and password on your database server and let them run raw SQL.
There are many possible security risks with this and it just isn't worth it.
(Aside: You can't make arbitrary socket connections with browser-side JavaScript, so it's impossible to connect to most database servers from it anyway).
If you want to expose data to JavaScript running in the web browser, then the standard approach is to write a webservice.
You can write the webservice in any programming language you like (including JavaScript). It listens for HTTP requests, reads data out of them, possibly performs authn/authz, the queries the database (applying the well-documented defences against SQL Injection attacks) and returns the result (often formatted as JSON).
The client-side JavaScript, therefore, just has to make an HTTP request (e.g. with XMLHttpRequest or fetch) with parameters passed in the query string or request body, and process the data it gets back from it.
Connecting to a database using client side javascript is very insecure as the javascript will need to know the login details. And since the client side javascript is on the client side, any user will be able to see the login details in plain text.
The best way to do this is to make a webservice on a server. When the button is clicked it will make a GET/POST request to the webservice with the entered number as a parameter. The webservice, which can be made using any language pretty much, will create the connection with the database and insert the row itself.
Although I would advise going the webservice route since it will be much easier to make secure. Playing with javascript to database is extremely dangerous unless you have a really good system and understand exactly what you are doing; but if you really want to do it and have an application that requires it, then can use PouchDB connected with CouchDB.
PouchDB is run locally and can sync with CouchDB over HTTP.
https://pouchdb.com/
https://couchdb.apache.org/
There is an answer here discussing basic security with pouchDb synchronizing with couchDb. Basically, each person needs separate login credentials and credentials should never be stored in the page code.
PouchDB security
There are some neat uses for pouchDB: https://pouchdb.com/users.html

Javascript and Python - Best communication method

I am trying to make a html/javascript controlled raspberry pi robot.
So far, I have installed a LAMP server and I am hosting a local webpage with buttons which trigger events and sends commands to my robot via AJAX. AJAX calls a php script which calls a python script (no CGI) to control the robot.
This solution is currently working for me and I can control my raspberry pi hardware from html and simple send data back and forth.
Now what I want to do is, on my web page, have an on/off button which initializes and shuts down the robot, as well as having other events (currently measuring tilt angles from an accelerometer in an android device) to control the speed of the robot. This is where I am getting confused by the procedure of how things will work.
When I make an AJAX call to start the robot, that ajax call does not close until the python script ends (robot shuts off). So I have a python script running (start.py) which contains my robot object where the methods for changing the speed are stored. If I have seperate events in JS to change the speed (sends a call to changespeed.py), how can I change the properties of an object which is stored in a different python script (start.py) that is already running (See figure below)?
Possible solutions I have thought of thus far:
1) store the object somewhere in memory. Perhaps as a python object, or store its properties in a SQL database. This way, changespeed.py (or Javascript directly) can change the speed properties in that memory, and start.py can continuously read them.
2) Rather than having the new speed values sent to python through AJAX, let python try to retrieve the values from javascript variables. in other words, instead of javascript giving data to python, have python take the data from javascript (scraping?).
3) abandon my current method of having javascript and python communicate through AJAX and php, and set up a websocket to exchange data. I dont know how this works quite yet but it may be the solution I need.
4) I also have not done any programming with interrupts and I dont know much about it. Is it possible to have my main script running and when the speed needs to change, I can send an interrupt at any moment?
I am sorry if my question is a little vague or wordy. I have a lot going on in my head right now. Let me know if it is unclear. Thank you.
TLDR: what is the best way to have javascript execute python scripts and then feed data to that script asynchronously
Map of operations
You can use cgi — Common Gateway Interface and By the use cgi-cgi.FieldStorage() you can able to get data in python .it support Huge data transmission and fast.it spport all the browser
It sounds to me that you are looking for an in-memory data store such as Redis. With redis you can create a variable in memory which can be accessed by any programming language that has support for Redis (pretty much all of them do).
Redis also has Pub/Sub functionality which allows you to execute specific logic when data is published to a specific channel. For example, if your javascript program changes something in redis, your python program could be subscribed to that specific variable, and know to execute it's own logic when the javascript program changes the value of that variable. For more information on pub/sub check out this page
I have successfully set up a websocket between my webpage and my robot using Flask-socketIO. I am now able to control my robot using the accelerometer in my android phone, but the response from the robot is way too slow. sometimes it takes up to 2 seconds for the robot to respond to a command from my phone. I believe part of the issue is that my robot is connected to my WLAN through a wifi extender in my house. So rather than passing data through the wifi extender, to the router, back through the extender, and finally to the robot, I am planning on using my 2nd wifi antenna as an access point, and connect to the robot directly.
Eventually I plan on getting into android app development (I want to connect my 2nd smartphone to the robot via USB and have access to the accelerometer, magnetometer, GPS, and camera). Once I do, I will look into connecting my phone and robot via bluetooth rather than websocket.
I still dont know if this is the best method, but this is my solution at the moment.

javascript - how to accomplish getting variable (data) from client side to a database online server?

so on client side running in the browser I have a javascript code that has a variable (namely a url that is 1500 characters long), and which I need to insert it into a online database that lives on the webserver where I have hosted my website. I have these two technologies on my website, mysql DB and PHP.
Please kindly would someone recommend the best way to do this?
showing examples, specifically, how to send this data over to the remote server and how to process return data it may send back to me??
what i was thinking if there's a way to send over this variable string that is 1500 characters long, over to a PHP file living on my website which this PHP file will be able to insert the data into the DB, and then some time afterwards my same script running on the client browser will check and pull data from the remote DB back to itself...... I've tried to follow along some example searches googling but none of them are making sense to me, sorry I am visual learner , and would greatly appreciate any help you may provide me with this task .....
The solution already discussed here is the proper one. You need an API (also called a service).
I don't know who downvoted it but its the right one.
And you need it for several reasons.
Performance issues. Your solution "writting to a file" will be slow. And even "writting to a file" will require a service on top.
Security reasons. To allow in any other kind of way for a user to write in your server directly (FTP or other methods) is a big security risk and your server might end up being attacked.
Scalability and mantainance.
I would recommend reading more at
https://code.tutsplus.com/tutorials/a-beginners-guide-to-http-and-rest--net-16340
And if you are a bigginer an want to start something fast loopback is an amazing option, but you need NodeJS in your server.
In broad terms what you need to do is set up a API on the PHP side of things. Basically you want a structure where your javascript can send the request, and then, using promises, wait till it gets a response to get the data. That way your PHP server can take however long it needs to put the data in the database and process it properly.
Here's a tutorial on how to make a restful api in php

Send data from client to server and fill out form by using JS?

Is there a way to send data from a client to the server using only JS and then fill out a form on the server and then the client gets redirected to the server and see a prefilled form?
I'm thinking using ajax sending a json object. But I'm not sure that will work.
I believe you misunderstand the concepts of server and client. The client is the computer you are using, the server is the computer where the website service runs.
PHP is your server-side code, which runs on the remote computer which serves the requests of the user computers.
Javasceipt is your client-side code, which runs on the computer of the users.
You need to create the HTML structure of your form as part of the HTML which is generated on your server. In your HTML structure you can have some small PHP scripts where you "inject" programmatically calculated values. Read more here.
In general, if you create a form, you not necessarily need AJAX (Asynchronous Javascript And XML). However, for advanced handling of your forms you might need to use AJAX. There are a lot of references, just use your search engines and view a few tutorials.

how does comet work with php?

when i use comet iframe i just send script tags from backend php file to front end and javascript is displaying it.
can someone explain briefly where a comet server comes up in the picture and how the communication will be between frontend (javascript), backend (php) and the comet server.
cause i read that if you are going to let a lot of users use your comet application it's better to have a comet server. but i dont quite understand the coupling between these parts.
use this link:
http://www.zeitoun.net/articles/comet_and_php/start
That is the best tutorial i could found, and takes 1 min to try;
in short:
( image from that tutorial )
index, can be html or php, creates a request, which php doesnt answer until there is data to send back, with chat, when someone sends you a message.
If you have many users chatting, i recommend using a java chat app
otherwise your server will load up with running php engines ( each unanswered request keeps a php engine alive, which is server capacity ).
http://streamhub.blogspot.com/2009/07/tutorial-building-comet-chat.html
this should help you out with that, but you do need java hosting :)
have fun
edit:
just read the other server part; sending requests to your own server can get messed because the timeout function may not work well, so the server crashes, an independant server timeouts the connection after a certain amount of time, no matter what.
I have a very simple example here that can get you started with comet. It covers compiling Nginx with the NHPM module and includes code for simple publisher/subscriber roles in jQuery, PHP, and Bash.
http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/
A working example (simple chat) can be found here:
http://cheetah.jamieisaacs.com/

Categories