![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
Videochat Solutions
Industry Role:
Join Date: Aug 2004
Location: Canada
Posts: 49,080
|
Question for Programmers: What's a good way to see if a browser connection to a server is active?
What's a good way to check browser connection with server to see if its still active?
For example: Let's say you have a bunch of people connected to a page on your website, and you want to know in real time or near real time, who by username is connected, and when they click away. Suggestions appreciated!
__________________
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
So Fucking Banned
Industry Role:
Join Date: Apr 2003
Location: online
Posts: 8,766
|
if its wordpress, there are plugins for that...
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 |
Videochat Solutions
Industry Role:
Join Date: Aug 2004
Location: Canada
Posts: 49,080
|
No, but they could be useful too. Do you know what they are called? Links if you have them..!
__________________
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
Confirmed User
Industry Role:
Join Date: Apr 2010
Posts: 1,101
|
You can use ajax for that... simply fire it every, let's say 5 seconds and call a PHP script with it that will update the users last online time / date... with this, you'll see who's "last_online" time/date was updated recently, which means they were online in the last 5 seconds...
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 |
Too lazy to wipe my ass
Industry Role:
Join Date: Aug 2002
Location: A Public Bathroom
Posts: 38,614
|
Years ago, in about 02/03 I was shown a website stats thing that showed all the users on your site, in real time, and what they were doing etc... But it wasn't just 'dry' text. It was actual animated little people, and the pages of the site were represented as rooms in the building and as someone moved from page to page / room to room, so did the little animated figure on screen.
A bit like very early sims but for website. Dunno what it was called, but it was pretty expensive apparently ![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 | ||
Videochat Solutions
Industry Role:
Join Date: Aug 2004
Location: Canada
Posts: 49,080
|
Quote:
Quote:
__________________
|
||
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 | |
Too lazy to wipe my ass
Industry Role:
Join Date: Aug 2002
Location: A Public Bathroom
Posts: 38,614
|
Quote:
![]() ![]() ![]() |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 | |
Confirmed User
Industry Role:
Join Date: Apr 2010
Posts: 1,101
|
Quote:
Code:
<?php //make sure you are connected to your database! if(isset($_GET['username'])) { $username = $_GET['username']; $current_date = date('Y-m-d H:i:s'); //update the database with the current date/time //for example //mysql_query("update `users` set `last_seen` = '$current_date' where `username` = '$username'"); } ?> <head> <base href="https://gfy.com/" /><!--[if IE]></base><![endif]--> <base href="https://gfy.com/" /><!--[if IE]></base><![endif]--> <base href="https://gfy.com/" /><!--[if IE]></base><![endif]--> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script> $(document).ready(function() { var username = 'username'; UpdateUserTime = setInterval(function() { $.ajax({ url: "http://localhost/2MuchMark.php?username=" + username, cache: false, success: function (html) { $('#foo').append('User time/date updated.<br />'); // you can leave this empty, it's just to see if the request was successful. } }); }, 5000); // the interval you want to update the current time / date. }); </script> </head> <body> <div id="foo"></div> </body> Let me know if it works for you, or you have any questions. ps. you are not limited to only date/time. you can store anything such as the current page if you want. for this I'd suggest using POST instead of GET, but everything is the same. Simply change the type of the request to POST and define the data you are sending with. Code:
UpdateUserTime = setInterval(function() { $.ajax({ type: "POST", data: 'username=' + user + '¤t_page=' + current_page, url: "http://localhost/2MuchMark.php", cache: false, success: function (html) { $('#foo').append('User time/date updated.<br />'); // you can leave this empty, it's just to see if the request was successful. } }); }, 5000); z |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 | |
KeepYourPimpHandStrong
Industry Role:
Join Date: Sep 2015
Location: Russia
Posts: 11,465
|
Quote:
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 |
(>^_^)b
Industry Role:
Join Date: Dec 2011
Posts: 7,223
|
If you use html5 Websocket with node.js instead of most other methods, you'll use much less bandwidth. Like a difference from xxMbps to xxxKbps for large traffic..
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#11 | |
Videochat Solutions
Industry Role:
Join Date: Aug 2004
Location: Canada
Posts: 49,080
|
Quote:
Thanks Sarn but that's not what I'm looking for. What I want is for my own program to know which users are actually logged-in in real time, and when they click out of the site. But thanks though I appreciate the suggestion.
__________________
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#12 |
Confirmed User
Industry Role:
Join Date: Aug 2013
Posts: 1,474
|
Ajax is a good solution if there are no load issues.
One caveat I'd point out there is that UPDATE queries tend to be quite heavy on a database, especially if you're dealing with hundreds of users online at any time, or updating on a large table. If the information does not need to be persistent, I would update something like a memcached key or redis object for each user, as those are much less intensive than running updates in SQL. Redis could be configured to save the changes to disk every X writes/minutes for some amount of persistence as well (though not as reliably as SQL, and then that's another database service you need to maintain/backup/etc) - just something to consider. If it is a busy site, I think it would be better to use websockets for this kind of thing, and Redis to persist data.
__________________
![]() MojoHost.COM | natalie at mojohost dot com | Skype natalie.ac | Telegram @znatalie. Since 1999: 70 Adult Industry awards for Best Hosting Company and professional excellence. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#13 | |
Videochat Solutions
Industry Role:
Join Date: Aug 2004
Location: Canada
Posts: 49,080
|
Quote:
__________________
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#14 |
Confirmed User
Industry Role:
Join Date: Aug 2013
Posts: 1,474
|
__________________
![]() MojoHost.COM | natalie at mojohost dot com | Skype natalie.ac | Telegram @znatalie. Since 1999: 70 Adult Industry awards for Best Hosting Company and professional excellence. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#15 | |
Confirmed User
Industry Role:
Join Date: Apr 2010
Posts: 1,101
|
Quote:
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#16 |
👏 REVOLUTIONARY 👏
Industry Role:
Join Date: Jan 2016
Posts: 1,440
|
Use websockets.
__________________
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#17 | |
Confirmed User
Industry Role:
Join Date: Aug 2015
Posts: 1,018
|
Quote:
First: The browser connection with your server closes the moment the HTTP request is completed. When you load a web page, as soon as the page is loaded, your connection with the server is closed. Nitpicky distinction, but it's important to understand it. Now, for your use case, there's two solutions, both of which have already been suggested: Ajax polling and websockets. Here's a high-level breakdown. Ajax polling doesn't keep a connection open with your server. It's just a little setTimeout loop that creates a NEW connection (ajax request) with your server every couple seconds. Your server can only communicate with users during the ajax request. Websockets keep an actual TCP connection open with your server. Data can flow both ways, continuously and freely. Your server can communicate with users whenever it wants. The big difference here is that, with ajax polling, your server can only communicate with the browser in its responses to each ajax request. With websockets, the server can communicate with the browser at any time. Think about a chat room: with websockets, the server can send chat messages to all users the moment the messages arrive. With ajax polling, the server can't send messages to users until the users make their ajax request (at which point the server sends the messages in the response to the ajax request) Ajax polling is compatible with (basically) all browsers and any server. Websockets are not supported in older browsers, and require an async server like nodejs or tornado. Ajax polling is much easier to implement than websockets (websockets aren't really that difficult, but it's not exactly trivial to get them communicating with your server properly) Based on the use case you've described, I would use ajax polling. Your server doesn't need to send notifications/updates to users in a continuous, real-time fashion. You just need browsers to tell your server if the page is still open. So, IMO, stay away from websockets for this. Unless there is part of your use case that you haven't stated here, that involves the server needing to send real-time updates to users. If your server needs to send updates to users, but it doesn't need to be real time (for example, maybe a 3-second delay is acceptable), ajax polling will be adequate. Also, as was suggested in this thread: when you store this information, avoid disk writes. If you have a thousand users on your site, all making ajax requests every couple seconds, you would be generating hundreds of disk writes per second. Not good. The information you're collecting from browsers here, it needs to be stored in memory and written to disk later, in batches. Good luck. |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() ![]() |
|||||||
|
|||||||
Bookmarks |
Tags |
active, server, time, real, connected, check, connection, browser, suggestions, appreciated, click, programmers, username, website, bunch, people, question, page |