process individual commands - javascript

So I am not sure if this is possible or if there is a better way to perform this task, but I am attempting to create a single ajax connection to a server-side script that basically relays function calls to the page by constantly listening to a named pipe. So, for example, I could do something like:
# echo "foo(param1,param2)" > /path/to/pipe/gui
which would pass the 'foo' function to the page to be executed. The ajax connection needs to be permanent basically (controlled via timeout?) or by using an occasional 'ping' to keep it open.
Currently I have a basic test, but I am not sure how to process each received call in ajax. I am using the readyState==3 to simple call an alert() with each passed string from the named pipe, but it seems to always return everything submitted. So if I have done:
# echo 'hello' > /path/to/pipe/gui
It will show an alert popup with 'hello' in it. If I then do:
# echo 'world' > /path/to/pipe/gui
It will then show an alert popup with:
hello
world
Again, I am not sure if there is a better way to approach this or even if there is some library already built to handle something like this. Any help would be appreciated!
UPDATE:
The comments say to use websockets and socket.io (which I think uses websockets), but I don't want to add a bunch of servers. Any solutions need to keep standard webbrowsers and web servers.

Related

Is SignalR suitable for getting result of a server side (background) processing?

I have a simple ASP.NET MVC web page in which the user uploads a file, in server side the file is processed, then some results must be displayed in the very same page.
I've picked ajax upload (so no page reload done upon [Upload] button) which works fine. The controller's action method with the file called, all OK. I am using an upload component, it seems the "success:" callback is not available for me to react.
* EDIT
Besides of the success callback seems not available, the processing takes 60 sec. I would like to return to the upload ajax call immediately, then process (optionally show progress on the client), and when the processing is complete show the result.
END EDIT *
My first KISS (an amateur) thought was: OK, then store the processing result in server side in session, and ajax poll from the client an action method what returns with the result. Btw it is not so KISS.
My second thought was: OK, then go to professional, and use SignalR. (I've never used). Now I am going though on the chat tutorial, and I have doubts: Is not SignalR overkill for this simple task? and also: Is SignalR suitable for this task at all?
I'm using SignalR for something somewhat similar: Generating and sending out a newsletter to 10+k recipients. This whole process takes about 5 minutes and the admin can follow the progress via SignalR.
SignalR looks complicated initially but it really isn't. One of the advantages I'd say is the ease of use. During your processing it is really simple to push updates to the client. Making it work in the client only needs a handful of javascript lines.
The overkill part here I guess is that you'll always only broadcast to 1 person.
Signal R can give the response to the specific client on success.. but yes its overkill. why don't you send a response(string or object) from server side after checking whether the data was successfully posted.This reduces the overkill of implementing signal R

Running code from Socket.io notification

I'm running a NodeJS server which is sending notifications to the clients when somebody does something, for example, when a client deletes a row from a grid, Socket.io informs the rest of the clients that a row got deleted.
In that example, I could add something like actionType: rowdeleted to the socket.io message and then just detect the actionType on the client side and refresh the grid. Anyways, the problem is that there can be infinite number of actions (and new ones can be added), so I can't code a function for each action type on the client side.
Then I thought maybe I can send some code via socket.io and make the client run it, but I'm not sure if that is the best way for doing what I want. Also, how are the clients going to run that code? Via eval?
I'm open to any suggestion :)
Have you considered something similar, but not as eval. You clearly must have the code to execute somewhere, be it on the server side. Why not create a way to let the client know what script/code/action to get and execute it.
I have used something similar out of a similar need. The action type referenced a script in a specific path on my server (/js/actions/ACTION.js). Upon getting the command to run the action, the client would check if it has the action, if not, it would go get the action. After that it would run the action on the script. RequireJS is good for this kind of thing. It will keep track of what actions you have and what actions you don't have. It will also make sure to get the action if it doesn't have it before it run some function that needs it.
eval is evil (c)
so I can't code a function for each action type on the client side.
there's no point emiting events from server if they wont be handled on the client(s)
have a client handle funcion for each type of event your server is emiting.
Otherwise bind on all events and handle then

Qunit Javascript client logging

we have small wrapper client logging framework to enable javascript client logging.We have exposed different function like LogError,LogWarn,LogInfo in Javascript.
JavaScriptLogger.LogError(exception)
{
postAjax(exception);
WriteConsoleLog(exception)
}
When ever user logs a error using JavaScriptLogger.LogError(exception),two things get executed:
1. postAjax(exception) : An ajax post is made to specified server URL.
2. WriteConsoleLog(exception) : Writes an exception Console.Error/Console.Warn/Console.Log,depending upon errortype passed by user.
Now I wanna have a unit test cases for LogError,LogWarn,LogInfo function using Qunit framework.
Could somebody provide me a suggestion how to start and how to go about.
Regards,
SCP
Well, first off, I hope you understand that someone could flood your server logs this way, and hopefully you have something in place to prevent this.
Second, you would want to mock that Ajax call so that you aren't relying on your server to work (eliminating one source of potential errors). You could use something like Mockjax for that.
After that, you would want to probably run all of those tests in an asyncTest call (versus a simple test) since the Ajax call is still supposed to be asynchronous.
http://api.qunitjs.com/asyncTest/
(PS Sorry if my formatting is bad, typing this on my phone.)

Javascript, .NET : show and consume an OK/Cancel alert

Here is my situation:
I am building a schedule asp.net app and i want to alert the user to the possibility of time collisions (if the user is inserting a new schedule for someone and that someone has something already scheduled for that time).
I have read and found lots of "calling JavaScript inside ASP.NET" articles here and out but they all are either showing how to call a function as soon as the user clicks something,
as soon as the page loads or just showing a general alert window with just the OK button.
My question is then, how can i call a javascript messagebox with ok and cancel buttons from the middle of a function /handler in c# and have access to whatever represents those buttons so i can branch accordingly? I hear jquery messageboxes are prettier than the regular ones, how would i proceed using those instead?
My question is then, how can i call a javascript messagebox with ok
and cancel buttons from the middle of a function /handler in c# and
have access to whatever represents those buttons so i can branch
accordingly? I hear jquery messageboxes are prettier than the regular
ones, how would i proceed using those instead?
This isn't really possible. Javascript happens on the client. c# function handlers happen on the server. They happen one after the other server > client > server > client, etc. You can't break out in the middle of server side code and go back to the client, and then come back to the server.
Everything that happens up to the point the form is posted happens on the client (unless there is Ajax involved), once the form is posted and you are in your c# code, everything is on the server, until the page life cycle completes and the response is returned to the client.
If you want to have a javascript confirmation function, it needs to happen BEFORE the form is posted to the server, and you need to store that result in a form field and pass it. Alternately, you can choose not to post the form at all based on the user's choice of OK or Cancel.

Call from JavaScript to server-side code in JSF

I'm looking for an easy way to call a bean's method that will take no parameters and return a string in JSF. The thing that I don't really need is that the method returns an action result and then uses the whole JSF life-cycle to do get me to another view. I need to do that from JavaScript so that I can put together some client-side parts of the application and going over the A4J part of RichFaces has brought me nothing so far.
So here's the scenario again in a step-by-step form:
from JS issue a GET on some address
on the server process that GET and return JSON or HTML (basically a string)
once the request is sent back to the client I want to be able to process it further with JS.
Thanks!
Use a4j:jsFunction and the data attribute.
So roughly you want something like:
<button onclick="callBackend();">Go</button>
<a4j:jsFunction name="callBackend" action="#{myBean.someMethod}" data="#{myBean.someString}" oncomplete="handleResponse(data);"/>
<script>
function handleResponse(response) {
alert(response);
}
</script>
Damo: can you explain why it might only work for the first time the method callBackend is executed? I'm experiencing a strange behavior that the first call succeeds and the next calls are just blocked. I see the server-side code being executed but some strange result is being sent back to the browser (something like the _viewstate and those kind of things).

Categories

Resources