Chrome Extension Requesting django server - javascript

I am creating a chrome extension that requires a back end server for processing. I would like it to send data to the server, the server will process this data and send some other data back to the user. Preferably this back end server should be in python as there are libraries I would like to use. Any idea how to go about this?
I was thinking to use a django server with rest API but I'm not sure what would be best. If anyone could provide a tutorial link or briefly explain the code behind this idea it would be brilliant.

Related

Is there any way to send http requests from JS by pushing button, to a C++

I am currently designing a GUI for a piece of hardware. We want the GUI to be able to be accessed over a browser. The browser would be displaying a map generated from C++ code, but I also need to send some params to the code that generates the map from buttons on the JS front end? Is there anyway to accomplish this. I have done a little research so far and know about web sockets and AJAX, but I am not entirely sure it is what I am looking for. In an ideal world I would be able to just send UDP packets, but my research tells me that is not possible, not is TCP. Is this correct?
Thank's in advance for any help!
Your C++ code could setup a server that listens for requests. You could use libhttpserver, for example. Then, in your JavaScript code, you can use XMLHttpRequest or the Fetch API (for newer browsers) to make an HTTP request to the server, which would then return a new static page (with the generated map embedded into the page).

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 server to client while page loads

If a Client is requesting something from the server, I want to send some extra information to the client along with the requested page. That web page is being processed by JavaScript on loading. Sometimes I require updating the DOM using the data which I have received from server. Can I get some pointers on how I can implement solution?
According to my understanding of your question. To communicate javascript to server and server to javascript you can use signalR. For more information please see.
http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr
Generally this method is used for chat application. I think for your problem this is one the solution.
please correct me if I am wrong.
I thing that websocket is your best solution, of course this only will work with modern browser (not IE, just IE 10), but if you are using nodejs in server side, you can use socket.io, http://socket.io/ they say that work even in IE 5.5+

Passing data to ruby script when executed through 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.

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