Synchronous Web Pages - php

I have a situation where an event have to affect several web pages on an event is triggered.
Its like in GMail. I am developing a project based on online stocking trading system for my college. There are asking for a facility where the administrator or the dealer doesn't need to refresh his end of the application to view the new orders placed by the customers at the other end of the application.
As I said its like GMail, wait for the event to happen, listen to it and update the page.
I am good at JavaScript and jQuery that I can do this within a page: event listeners. But how to do this across several pages.
You can also consider another situation: A waiter, a cashier and a cook in a hotel, all have a PDA with them, any of them doesn't have to refresh their instance of web page to update the orders placed.
How can I achieve this? Is this possible?
I say I am good at javascript but I have my limits and doubts.
I use jQuery and PHP.
Thanks in advance.
PS: I know how to refresh the page or iFrame for every 5 seconds or so.
Edit 1:
As for additional details, I want to something similar to below that is done in java
synchronised(sync)
{
sync.wait()
}
The code may not be accurate, this was from my lab experiments. But the concept is similar. Server waits till the client made a move and this is repeated for a lot of time.
Edit 2:
Another best example is a situation we always face in the internet: Complete a survey or something to enable a content to be downloaded.
My trading system needs such a thing. The dealer page always listens for the client page to act with the server. Upon such thing, the dealer page is reacted to that action. In this case placing an order.

If there is several page instances on one browser, you can use javascript web worker.
ELSE
Use "Long pooling" OR "ajax quering"
Long pooling demo: How do I implement basic "Long Polling"?
What you do, you always query server for new information. If there is new information, you return it. Then javascript will see that there is new information and it will show it.

Look into a long-polling or "comet" architecture. This is widely used by event driven AJAX applications and will provide what you are looking for.
See https://en.wikipedia.org/wiki/Comet_(programming) for more info.

Related

Creating A Website Status Page

Is there anyway I can create a page on my website (PHP if possible) that would show the current status of my website and when it has/will have down time?
For example, could be similar to Google Apps Status Page at https://www.google.com/appsstatus
Thanks in advance
The best solution is to make a PHP script which loads one of your pages, looking for a specific content : if it finds it in a reasonable time, you can consider your site okay, if it doesn't find anything, or if your site takes too long to answer, you can tell you have a problem, and take action (for example, sending an email).
This script has to be launched regularly, so a cronjob has to execute it every x hours or so.
And, of course, it has to be on another hosting provider than your first website, because if there's an issue with the host, you can't tell it if your check script fails to load.
Edit : if this is complicated for you, some services can do it for free, like this one I found just now : https://uptimerobot.com/

Extracting Data from Another Domain, possible?

I'm terrible at keeping track of my bills, so I wanted to create something automated. I also wanted the challenge of making it myself.
My questions:
Is it possible to have a webpage connect to another domain (any utility website i.e. timewarnercable.com) with the proper login credentials and retrieve the dollar amount I owe, then send me an email or even just display it on the webpage?
I've already got a webpage setup that has all my account info stored in it (don't worry it's only a local site!) and I can click a button and the info I have stored sends a POST request to the utility login site. This logs me in to my account page and then I can view the bill. But don't want it to open another page..I'd rather load the content of that page in the background, scan for the code where its says my $ owed, then capture that somehow, then return the dollar amount onto the webpage.
If so, is this possible to design this with Ruby (Rails) or php, with Javascript / AJAX.
Thanks!
What you're basically asking about is "page scraping", but your scenario is more complicated. You would have to fake the login post, capture and store any cookie/session info returned to you in the response and use that in subsequent requests to the site. You may also have to deal with redirects, depending on the site.
I have found nodejs actually quite useful for scraping pages since it has plugins that provide dom selectors (there is a jquery plugin) - you're using javascript for server-side programming.
Check if the site has API and if the site provides that, will make your life a ton easier.
Some banks like BankOfAmerica have applications that already do this - they aggregate your accounts and bills from other sites, see if your bank can do this.

back button in ajax supported pages

i have a master php page and now i am loading different pages in a div of this master page using ajax. now i want to add a back button on the slave pages so that the div can loaded with its just previous slave page but the back button, i am using uses history.go(-1) that take me away from master page. has anybody any solution. Thanks in advance.
the code i am using is written below. but it is not working in ajax pages
Go back
My suggestions would be to use a framework for this purpose like backbone.js
Where you can setup routes just like on server side.
I feel this is the best way to do this on ajax sites.
Read tutorial on Manipulating browser history.
It mentions:
pushState, which is used for creating "entries" in the browser's history (with attached data about state), allowing to get back to some specific state you previously saved,
onpopstate event, that is fired when you go back or forth through the browser's history, and passes information previously saved for specific entry in the history (so you actually can read data previously saved by pushState for this specific entry),
In case pushState and onpopstate do not work for you, there is still some ability to work on the basis of onhashchange event (however limited).
The good news is that what you want to achieve is definitely possible. It is even implemented in many famous sites such as Twitter, GitHub, Facebook etc.
You can use the history functionality provided in newer browser. There is several libs that already provide functionality for you:
For JavaScript MVC applications:
http://documentcloud.github.com/backbone/#History
For mobile web pages:
http://jquerymobile.com/demos/1.0b1/#/demos/1.0b1/docs/pages/docs-navmodel.html
For implementing a solution your self:
http://benalman.com/projects/jquery-hashchange-plugin/

How to update content based on user actions [like the facebook wall]

For my next application i would like to implement something that has a feature like the facebook wall but let me explain a bit. For those of you who used facebook you know that when somebody posts a message on your wall, and you are logged in to your account, you will get a notification immediately somewhere in the lower left corner. Lately they even pushed this a little bit further and if somebody comments on it the comments are updated as you visualize the page, it's like an instant chat.
My application will be developed in PHP, I will use Zend Framework to do it. I'm interested in the basic principle that makes the facebook wall behave like that (updates in real time). I know there is ajax involved but I can't really tell how is the javascript triggered when the user is doing something. Or even more, how to push back to a user some info that was added after he viewed the page. For example, let's say that a somebody adds me as a friend. I would like to see a notification saying "X has added you as a friend" if i am logged in. I hope you understand what I'm trying to do.
If you can tell me some basic ideas, maybe provide some links that have this information I would be very grateful.
Thank you for your time in reading this.
you need to look at comet , reverse ajax , ajax polling
If some event is triggered, then store the event on database (with ajax or without ajax).
You will be needing a script in server to check if some event has been triggered or not. This script should be able to check events that are stored in database.
You need to execute script in step 2 periodically. This can be acheived with with ajax (javascript or jquery) and a function settimeout (on javascript) to send ajax request to server periodically.
Changes are sent from server. So parse the response and update in page using javascipt and jquery.
So, it can be summarized as
Register an event (for one user)
Check the event (for other user)
Parse the response and update the page
There are several elegant ways to do this as answered by others.
The best would be the start the project and ask for help where ever stuck.
It is only partially possible to keep an HTTP connection open, so the best option is probably to poll for changes. You can send a request each second to see if anything is changed since time 'x'. On each response you send along the server time. With the new request you send the time of the old request and the server can return any events that happened inbetween.
Also you can read something about AMQP. You can send a message to recepients inboxes (after some actions in your system) and then read inboxes after start or with some time interval.

Record User Actions Using PHP and Javascript

I am developing an online learning system (PHP, MySQL and Javascript). I would like to track what pages and how long each users spent on each page. Ideally, I would like to record this in a MySQL database. My question is 2 fold:
1. What kind of fields would I include in my db table to record multiple pages accessed?
2. Is this problem best approached by server side only or by using javascript ? e.g. server side: hidden form fields with a page id attached, page id is passed to db and recorded?or Javascript: record all actions in Javascript variables and somehow pass to db at end of session?
Really I am just looking for some high level guidance on an approach as opposed to code snippets.
GF
PHP isn't my normal language, but I would think about creating a module of code that can be called from the top of each of your scripts, that basically logs away "I served this page, with these form variables, at xxx ... ". To be more precise, I would record that in a table.
If you need to know when the user left your page, for a page on another site or perhaps shut down their browser for instance, then a purely server side solution isn't going to cut the mustard. In that event, you are going to have to start thinking about JavaScript, and intercepting events - such as the onUnload event...
Have a read here...
While I know of no solution that can track individual users out of the box (I'm sure there are some), I am pretty sure you could customize Piwik to do this. Piwik aims to be a self-hostable alternative to Google Analytics. It is open source and build on Zend Framework and MySql.
Piwik collects usage statistics through a JavaScript tracking code and a Webbug image for fallback. Basically, what you would need to do is pass the logged in user's user id to the tracking script and then write a plugin that knows how to handle this information.

Categories