How to watch user requests from server? - javascript

I am developing a website using J2EE(JSP,struct2) and Apache Container. My website will be some kind of scial media webiste like facebook.
All the thing I like to do is to change the user login status in database to active after user login and change it again after user logouts. Those active user list has to be shown to other users as well.
All I am thinking is to change user active status as soon as user login to the website and will use ajax for up-to-date active user list. The thing I have trouble is that I want the server to check if the user is still active and still using the website every 5 seconds or whatever.
To do so, all I am thinking is to watch the user from server if it is still sending the requests to the server. How can I watch like that?? Where can I read the tutorials to write such cde.

You will need 2 parts to solve your requirements.
JavaScript on client
Ajax receiver on server
Client:
On the client side can you use a idle handler which sends a request after some idle time on the user side.
Choose one of this solution
https://www.startpage.com/do/asearch?cat=&query=inactivity+user+javascript
Server:
On the server side just receive the Ajax request and write it to a DB. Afterwards you can do whatever ever you need with this record

Two suggestions:
First: Using something like socket.io instead of AJAX in order to make it more
lightweight on the server side.
Second: Consider delegating the inactivity detection and announcement to the client.

Related

Can I instantly print information to the database when sessions close when the user closes the browser?

I have a project that I am developing with php. When users log off, the query runs on the MYSQL database, for example: status = 'offline'. But I also want to do this when the user closes the browser. For example, I want to update the query in the MYSQL database instantly when the user is disconnected from the server when the user's computer suddenly shuts down, such as status = 'offline'. To illustrate this, you're texting someone via facebook. The other person's computer suddenly shuts down and, after 1 minute, he is writing offline at the top of the conversation. I want to do something like this, I've tried various methods but failed. I'm new to PHP, can you help me in detail?
A possible solution maybe to update some field in DB saving the most recent user's activity time. It can be done simply by updating this field on every request from client.
Than you can consider user as "offline" in case of not receiving updates within X minutes since the last update.
You can also add some client side code which will send background "keep alive" requests once in a while to inform server that client is still there.

Button click that stays active for all users

Looking for some help with a problem i am facing, I am working on a project where we have a page that contains buttons with statuses, for example full, empty, half-full etc, These are designed to show that status of trucks as they pass through several systems, as they do, 1 of many people can select a button and it should update on all screens.
So something like this.
Truck enters stage one, user clicks button on stage one section of the page and that is then displayed to everyone who views the site on the internet.
I have no code at the moment as it is still be planned out.
I am stumped on the best way to achieve this, so any help at all would be appreciated.
Thanks
Edit: Alternately, use WebSockets, which solves the issue I describe below by allowing server and client to interact two-ways. Have a link!
Think of a web application in terms of server and client. The server cannot send requests; only the client can send requests to the server, which can respond to these requests.
In your application, the server should at all times keep track of the state of all buttons (e.g. whether they are pressed or not). Whenever a client presses a button, it sends a request to the server, which now knows to change the state.
The problem now is sending this new state to all of the other clients. Since the server can only respond to requests, not initiate them, that means all of our other clients must be waiting on a response from the server at all times.
Thus, the solution is to have each client constantly have an unanswered request to the server. Whenever the server responds, immediately send back another request (anticipating the next state change).
Tangentially: this is how many real-time chat applications work, including Facebook chat. If you open the developer tools on your console, you can actually see your browser sending and waiting on requests.

How to facilitate a no-polling messaging between two users on PHP

Server: NGINX/PHP running Laravel 5
Let's assume I have two users on different computers with sessions to the application.
User 1: makes an ajax call that is handled by one of the server controllers.
User 2: needs to get notified of this as soon as user 1 made this call to the controller.
Now, user 2 can have a javascript polling mechanism which asks the question "has this happened?" repeatedly, but ideally I'd like to avoid the constant calling and have him/her notified upon occurance instead.
Is there any way to have like an "open socket" for user 2 to be notified when certain events occur in the controller?
Ideally I'd like to avoid installing a third party messaging system, XMPP Etc on my server. Is there any best practice where this functionality can be achieved?
Sockets.io is indeed a good solution for this. I'll be using it on both the server side (Separate NodeJS server for this), and the client javascript side.

An indication of whether the user is still on site

I'm looking for an event that will return when close page whether the user is connected to my site.
onbeforeunload does not help me because it is also activated in efresh, submit, browse from page to page on the same site.
There is no client side event that triggers when a user leaves your site for another site specifically.
If you want to know if users are online you can make repeated ajax calls on a timer, and use a server side session that updates a timestamp of the last request. Using this you can query the timestamps for example, get sessions where the timestamp is in the last 2 minutes.
If polling is an option and you have a server-side backend, you could make a keep-alive script polling the server every x seconds and add it to all pages on your site. If you do not receive enough calls (i suggest at least 3 or so), consider it as a signal the user has logged out. Of course this won't provide an immediate feedback, but should be reliable enough.

How to pull new post and comments

I am currently developing a website where many users post topics and the associated topics' comments are shown in the page. Currently I'm developing using cake php.
The very first time a user clicks the website, all the topics and comments are displayed. But when other users add new topic or comment to a topic, I need to show the update in the same page. I am confused as in how am I able to retrieve new contents and update accordingly in a page. For instance how facebook does it where when your friends adds status or comments on your status, it updates without refreshing the page.
I know that AJAX technology is used but how is it done. Any source that I can refer? Hope someone can help as I have been doing research for the past one week but no answers so far.
You can go two routes in this.
Server Push
http://en.wikipedia.org/wiki/Push_technology
This technique is perhaps the most efficient as the server notifies the client of any updates. However this technique usually requires a bit more work than a simple polling system. You can use something like nodejs or Comet to push updates. If you're using nodejs, I highly recommend using SocketIO to handle the client side. With Socket.io you can have the client side listening to the server on a channel so that the server can notify the client whenever an update happens.
Client polls server
In this version, the client (new visitors browser) constantly polls the server for updates. You can set whatever gap you want, but keep in mind that if you make the polling gap too small your server might take a performance hit as each new user will create many requests. This method is as simple as setting up a setInterval() call in JS coupled with an AJAX call.

Categories

Resources