php: two client to share server user defined variable - javascript

GOAL: create kitchen and front desk can notify each other.
Q: how do i create php variable that can be read and set from kitchen and front desk. Let say the variable is vStatus. so the plan is when the front desk create order then vStatus = 'kitchen1', and when kitchen notify order is ready then vStatus='frontdesk1'.
on each other i create timer function to monitor the vstatus:
on front desk when vstatus='frontdesk1' then page is refreshed, and set vstatus=''. so the timer function on front desk side cycle continue until vstatus=frondesk1.
on kitchen when vstatus='kitchen1' the kitchen page is refreshed and set vstatus='' and timer function on kitchen side cycle until vstatus='kitchen1'
the hardware setup is like this:
front desk :apache web server, web browser
kitchen : web browser

You cannot share a PHP variable between separate requests - that would be a terrible thing from security point of view.
What you are looking for is a storage - some place outside of the php process that would persist your value even when the request processing (php execution) ends.
There are numerous options available for that, but here's a few most common and easiest to setup/implement:
Files - e.g. at the beginning of the execution read the value from file and when the value changes, write the new value to the file overwriting the old value
Key-Value store (e.g. Memcached or Redis) - You would need to install the memcached/redis server that would run as independent system process. Then you would connect to it and read the value and write the value when it changes. (Note these can be configured to be persistent or non-persistent storages, see next point or explanation)
APC cache - it's a special case of key-value store which works as a PHP extension rather than external service. The idea is still reading/writing the value stored in it. Note that this is not a persistent storage, meaning that when apache or php-fpm process is restarted the cache will be cleared and the value lost.
Database (e.g. MySQL or MongoDB). Again, you would have to install the database server and do some reading on how to use them.
There are certainly more options, but these are something to start with.

You need to use SQL to store that variable on server and AJAX requests to if you want to check the value in any set interval

Related

Javascript save variable outside of program

I am writing a simple iterator script in a third party program:
var i = 1.
function run(){
output1 = i;
i = i + 1;
}
This function is run for every record. But the program works with batches of 25 records. So for 100 records it outputs 4 times 1 to 25. I have access to the java libs. It is a linux server. Can I put i in some kind of global java variable or memory, so i can live outside of my program? I tried reading and writing i to a text file. This works but is to slow giving me some empty's, ones and two's. Making the program wait would be my last option (how can I do that?
I have no other easy way to add these numbers. I am limited by the program Oracle EDQ options.
Unfortunately you can't your Java memory resides in the memory of your server whereas your javascript variables are part of the memory of user's browser i.e. user's system. So you can't have share memory but you definitely can establish link between your server and client through http.
So if you need to save some javascript variables on your server, you can initiate an ajax request from client which will pass the data to server. Once you get it there you can do whatever you like to do with it

How to save a Javascript variable as serverside variable so everyone can see it

Hey I have been looking around for like 2 hours now and cant really find what I'm looking for. I want to turn a JavaScript variable into a server-side variable so that when it is changed it is changed for everyone visiting the site. I have tried looking at Node JS, XML, PHP and SQL but i have absolutely no clue which one is the one I need to do this. If any of you guys could give me something to research in order to accomplish this I would be grateful.
My code is a voting function that just increments a variable by 1 and I want it to stay as that variable when refreshed and visited by others.
JS:
/*global*/ buttonText = "Global Clicks: 0";
/*global*/ amountOfvotes = 0;
function addVote() {
var newButtonText = buttonText.replace("0", amountOfvotes++);
document.getElementById('global-respects').innerHTML = newButtonText;
}
Thanks!
There are a lot of solutions that you can use for your problem. The first two things which are in my mind are a (1) classic client-server communication, and (2) the usage of web sockets. Well, I am not so firm in the usage of web sockets, so I describe the "easier" client-server communication.
An information before I start: The tools and programming languages are mostly independent. However, you can use JavaScript on the client and PHP on the server side for example.
The first, you need, is a (e.g. JavaScript) variable on your client. You had called it amountOfVotes (cf. the picture at the end). There are two things happen during the client is active: (1) if the client add a vote it has to send it to the server; (2) all clients have to stay up-to-date.
If the Add Vote button is clicked, then you send a message (e.g. via Ajax) to your server application. Then, the server takes its current amountOfVotes, adds one vote, and stores it (e.g., in a file or mysql database).
Your clients need an intervalled function. This function sends (e.g., via Ajax) a message to the server to get the current amountOfVotes. The server's responds with the current number and the client is up-to-date.

How to retain (per session/tab) the uploaded file in server (Nodejs/Java script backend)

My requirement - Single page application having a File upload and many other sliders/dropdowns that fire events to server whenever there is a change.
Ideal workflow for my application is
1) User uploads file using File Upload widget-> server should retain this file and use this for further requests from the same client
2) User changes sliders or dropdown values -> Client fires requests to the server. Server makes use of the file that was uploaded initially and applies some logic and returns a response that is rendered somewhere on the page.
User can simultaneously open the app in multiple tabs on web browser and each tab should be isolated, i.e., user can upload different files in different tabs and server should retain files and respond respectively.
Please let me know what technologies/frameworks are best fit for my use case.
Any links/example pages having the similar use case would also be very helpful.
I'd suggest this short of scheme:
When you upload the file, the server stores the file somewhere, coins a unique ID for that file and then stores that unique ID such that it can associate that unique ID with the recently uploaded file - probably using a simple database to associate the two in a persistent manner.
The unique ID is then returned to the client after the upload. The client window then keeps track of that unique ID in a javascript variable for that window.
Whenever that window wishes to apply some operation to that file, it sends an Ajax request to the server with the file's unique ID and parameters for the operation. For example, an ajax request to crop a file like this: http://x.com/api/crop?fileID=19482302&x=0&y=0&h=1024&w=2048.
When the server receives the Ajax request, it uses the unique ID to find which file it is associated with, applies the operation and returns the results.
The client receives the results and renders whatever it needs to render to show the results.
To keep unused files from building up on the server, the server probably needs to "age" away files that are no longer in use. For this, you can create some sort of expiration. When a file is accessed, it's recent access time is recorded. Then, you can have some periodic timer (say every hour) that cycles through the files and remove any who haven't been accessed recently (say in the last day or 6 hours or whatever time is appropriate for your app).

browser tab wise session creation using JSP ans Servlets example [duplicate]

I'm developing a single page jQuery & Backbone.js web app. The backend is a JBoss 6 application server.
Until now we had the following structure:
There is only one servlet (front controller). Every request from the JavaScript client goes through here.
In the servlet - at the first request of a certain JS client - I make a look p to a stateful session bean. For the next requests of this client, I store the result of the look up in an HTTP session container. So every JS client has exactly one stateful session bean. This connection is kept by a session cookie.
Now I have an additional requirement:
When the user has two browser tabs (in one browser), they should have two isolated instances of the web app in every browser tab. Because of that I have a problem with session cookies because this session cookie is for all browser tabs.
I have to change the structure so that:
The servlet has to generate a new session ID for the first request of a certain JS client. This session ID is communicated to the client.
With every POST to the backend the JS client has to send this session ID.
My question is:
Until now I saved the result of the look up in an HTTP Session object and I hadn't to think about generating a session ID. But now I have to store this somewhere else, where?
Has anybody experience with this kind of setting and can help me?
Update:
Thank you BalusC for this very interesting approach.
When I understood you well, this means:
All individual JS clients of the tabs of one browser share one HTTP session object. And in this HTTP session object, every tab has its own entry point. That sounds really good. So I still can use the whole HTTP session infrastructure and don't have to reinvent the wheel.
Autogenerate an unique value on the initial GET request which you store and pass around on every subsequent postback as a hidden input value. Use this unique value as identifier of the session attribute representing the view-scoped data.
During the 1st request on a brand new session, do:
Map<String, ViewData> viewScope = new HashMap<String, ViewData>();
session.setAttribute("viewScope", viewScope);
(the ViewData represents the view-specific data you'd like to track across postbacks on the same view)
During every GET request, do:
String viewDataId = UUID.randomUUID().toString();
viewScope.put(viewDataId, new ViewData());
request.setAttribute("viewDataId", viewDataId);
During generating the HTML, do:
<input type="hidden" name="viewDataId" value="${viewDataId}" />
During every POST request, do:
ViewData viewData = viewScope.get(request.getParameter("viewDataId"));
// Get/set view-specific data in there.
Make sure that jQuery also passes this hidden input around (which shouldn't be a big problem if you already properly use $(form).serialize() or e.g. AjaxForm plugin to ajaxify the forms).
If you're familiar with Java EE's MVC framework JSF, then it may be useful to know that its #ViewScoped annotation works roughly the same as described above. See also a.o. How to choose the right bean scope?
You can use session tracking with URL rewriting. See here:
Session shared in between tabs

Differentiate browser close event and logout

1) I need the following requirement to be satisfied:
Client's request(Long running process) should wait till the server is serving the request.
Current solution:
Client initiates the request followed by ping request every 5 sec to check the request status and
with that also maintains the session.
2) If the client moves to other tab in the application and comes back, The client should still show the process status and server should continue working on the request.
3) If the client closes the browser or logs out, the server should stop the process.
PS : Need the functionality for all the browsers after IE-9,Chrome and Firefox.
There are many ways to skin a cat, but this is how I would accomplish it.
1, assign unique identifier to the request (You most likely have done this as you're requesting the ready state every few seconds).
Set a member of their session data to the unique ID.
Set all your pages to load the JS needed to continually check the process, but the JS should NOT use any identifier.
In the script that parses the ajax request, have it check the session for the unique identifier, and update an internal system (file or database) with the time of the last request and the unique identifier.
and push back details if there are details to be pushed.
In another system(like a cron system) or within the process itself(if in a loop for example) have it check the same database or file system that gets updated with the timestamp for the unique identifier and the last timestamp. If the timestamp is too old, lets say 15 seconds (remember page load times may delay the 5 second interval), then kill the process if cron'd, or suicide the process if within the process script itself.
Logout will kill the session data, thus making the updating of the table/file impossible(and a check should be there for this) and that will make it so that in the next few seconds from logout, the process stops.
You will not be able to find a reliable solution for logout. window.onbeforeunload will not allow you to communicate with the server (you can only prompt the user using only the built-in dialog, and that's pretty much it). Perhaps, instead of finding a solution on capturing logout/abandon, add some logic to the server's process to wait for those pings (maybe allow 30 seconds of no-comm before abandoning); that way you're not wasting server's cycles that much and you still have the monitoring working as before.

Categories

Resources