Tornado Bidirectional communication without websockets - javascript

I'm building a Tornado based server that basically allows the user to upload an image, does some processing on the backend and returns some updates during and after the processing.
I've implemented a basic server using Handlers, which works nicely.
The problem is that the handler interface doesn't allow me to communicate with the client, but only to re-render the entire page.
I've considered using WebSockets, but from what I see they shouldn't be used for image uploading, so it kind of kills this option.
Is there any other way to communicate with a specific client from an Handler (i.e render only part of the page, trigger some js event and so on).
Thanks :)

Are you using POST and GET methods in your handlers?
If you're using a GET method to receive the image from your client, you can communicate with the client by returning data using the self.write(json_data) method. (http://tornado.readthedocs.org/en/latest/guide/structure.html) However, once the GET method returns the request is considered to be finished, so you might not be able to send multiple updates.
Also, can you also configure the client side? I'm assuming you're using a JSON GET method to make a call to the tornado server, and in that case you can just link certain responses to different js functions in the client-side code.

Related

How to communicate both ways between embedded browser and webside

I have a C++ project for windows, using MiniBlink as embedded browser. (MiniBlink is a smaller Blink, which is close to chromium). I use this embedded browser to show responsive and nice looking dialogs with Quasar.js (wrapper for vue.js).
Problem:
Mostly a browser is just the passive backend. In my case, both the backend (project with embedded browser) and the frontend (dialog) are active and thus I need some communication. At the moment I use a local server to catch HTTP send from the frontend to the backend.
But is there a way to communicate from the backend to the frontend? At the moment I could only think about catching cookies or using a permanent loop in JS to send http queries to check for a possible response.
And is there no other way to send information to a backend? Everything is local, I dont need nor really want to send it into the network.
Thanks!
Idea 1: Use a local temp file to save on one side and read on other (can be also used both way)
Idea 2 (similar to question author solution): Local server with both side communication (GET/POST request into one side, text/json other way around)
Idea 3: Use launch parameter to pass though data directly into links for example: instead of using browserprocess.exe file.html, use browserprocess.exe file.html#showsomething
There are also other ways which like catching for example: checking window title of process with certain binary name from running tasks by other side; we didin't get good enough info about your background becouse you coud either use it in same process or other process, if thats same process you coud also just directly use variables both ways directly in code of miniblink and do action when they meet if statement
As CertainPerformance added as a comment, WebSockets might be the best way to go.
If one does not like to implement a websocket server, because a http server is already running, long polling requests might be the best workaround to simulate this behaviour.
Long polling: The client sends a request, which stays open as long as possible. If the server needs to communicate, it can use the open request to send its own "request" via response. It is a bit hacky, but essentially the idea behind websockets.
Mozilla has a nice article to help with websockets:
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers
If you (like me) use vuejs or quasar, you might want have a look at vue-native-websocket.
https://github.com/nathantsoi/vue-native-websocket
Good luck

How to call RESTful web service automatically

How do i call a NodeJS RESTful Web service automatically when a data is stored in Google Realtime Firebase? For example, once i stored user details to firebase my restful webservice should detect the data and perform changes on the data. My code works perfectly fine but my question is how do i make it perform automatically without me running the server remotely.
You can do it using two ways:
(1) Traditional approach:
you can implement polling, basically calling your rest API again & again at a
certain interval; get the data & check for change
(2) Websockets(I like it more):
You can also use WebSockets, basically, web sockets can maintain a live two way communication with the server
. Reference on how to use WebSockets can be found here
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
I think that you can use an event listener and depending on the event a specific part of your code will be executed. Check the documentation for what suits you https://firebase.google.com/docs/database/admin/retrieve-data

Communicate between client in Javascript without server

I'm searching a way to communicate between two client in Javascript/HTML5, without using a server-side script. I can't use NodeJs for this.
Is there a way to connect two clients?
What exactly are you trying to do? Can you use dependency injection?
I've had to do some workarounds in the past where I've opened windows or iframes into a target page using a url configured like a GET request, complete with a "?key=value" tacked onto the end. The target page has some vanilla javascript in it that looks for these faux GET requests, parses them, and either performs the requested action (fill out and submit a form or click a button or whatever) or uses the window.postMessage to send the requested information back to the original page.

How to run javascript file on the server side?

Suppose I have server. A client loading an HTML file containing a javascript library will have the script executed by the browser. The problem here is that if the client's computer is slow, the processing will take a long time.
So I want to move the processing to the server side. But, instead of having to rewrite the entire javascript library into another language, I simply want to run the javascript on the server.
Googling "server side javascript" directs me to Node.JS, which in my imagination have the capability to do so. But, I cannot find a tutorial which does just that. Does this mean that there really is no easy way to do so? For example, because the javascript script may contain DOM specific things such as document.getElementById(), which does not make much sense on the server side.
There is no trivial way to simply shift processing of JS from the client to the server.
You need to break the code down into code that must run on the browser (such as, assuming you don't want the browser to load an entirely new page, DOM manipulation) and code that can run on the server.
Then you need to provide a way to pass data between the server and the browser, this is normally done via HTTP (using Ajax).
When you take input from the client you need to send it to the server in an HTTP request (instead of just passing it as an argument to a function). The server needs to read the HTTP request, process it, and make an HTTP response.
The Ajax callback then needs to parse the response and run any client side logic (such as DOM updates) in response.
Note that network communication times will impact performance.
You can't "merge" the client and server in this way. All you could do is process the data on the server and just display it in the client without any further processing. Maybe you should refresh you knowledge about HTTP and how websites are send to the clients. Without any additional tricks, like websockets, comet or ajax polling, you can't access the client after you send the initial website to it. Even than you can just send data to the client.
When you want to stick to Javascript, Node.js is a good option. But even than you would need to send the data you want processed to the server, process it there and send back the processed data in JSON or "display ready" HTML.

How to Stream data from WebAPI method to a JavaScript client

I have a service method written in ASP.Net WebAPI :http://diningphilospher.azurewebsites.net/api/dining?i=12
and JavaScript client gets the response and visualizes it here
But the nature of Dining Philosophers problem is I never know when the Dead-lock or starvation will happen. So Instead of having a request/response I would like to stream the data through service method and client side JavaScript read the data I assume JSON asynchronously. Currently several post directs me towards changing the default buffer limit in WebAPI so you get a streaming like behavior.
what other(easy or efficient) ways exist to achieve this above behavior.
You can return PushStreamContent from ASP.NET Web API and use Server Sent Events (SSE) JavaScript API on the client side. Check out Push Content section in Henrik's blog. Also, see Strathweb. One thing I'm not sure about the latter implementation is the use of ConcurrentQueue. Henrik's implementation uses ConcurrentDictionary and that allows you to remove the StreamWriter object from the dictionary corresponding to the clients who drop out, which will be difficult to implement using ConcurrentQueue, in my opinion.
Also, Strathweb implementation uses KO. If you don't like to use KO, you don't have to. SSE JavaScript APIs have nothing to do with KO.
BTW, SSE is not supported in IE 9 or lesser.
Another thing to consider is the scale out option. Load balancing will be problematic, in the sense there is a chance that the load will not be uniformly distributed, since clients are tied to the server (or web role) they hit first.

Categories