Interact with website visitors in real-time? - php

I'm trying to set up a page where users can perform different actions on a webpage while chatting with me. I need to be able to have a live view of a particular DIV on the user's page that has tabs. When I click a tab, it should update on their screen and when they click a tab, it should reflect on my screen.
Finding a chat script was easy enough, but I'm struggling to locate on Google and Stack Overflow a basic script or code snippets to acheive this. Perhaps I'm not using the correct terms. Could someone please point me in the right direction?

I think what you need is socket.io. It's really easy to use: http://socket.io/#how-to-use

There are many ways to hack this into working, but, if you want a standardized way of doing it, use the Bayeux protocol. Way that this kind of communication is done by gmail, facebook and so on is via an implementation of cometd (cometd.org). There is a lot of reference material for implementing this setup on the net.
By going this route, it's going to be a two part problem: (1) setup the environment to allow for cometd interaction
This could basically be 0 work, provided that your remote host gives you ability to run daemon scripts ex: infinite PHP scripts.
(2) write the code that will sync clients with server. This will be the low end part where you read the div, and make broadcast-level communication to all clients about it.
A good fully operational example is this How to implement COMET with PHP article.

Related

How can I update a page in real time in reaction to database changes?

Thanks for helping.
I have no formal experience with web design and would like to consult experienced individuals for design recommendations rather than devising a convoluted solution myself. In a current project, I must prompt GUI updates to a webpage in response to every column altercation within a database.
I have found several recommendations for similar problems, but remain too inexperienced to truly understand whether each is viable for my specific issue or how to fully follow through with each.
In brief, how should I go about completing this task? Thanks again.
There are two options.
Ajax
Web-Socket
Choose which is suitable for you,
AJAX
Ajax is a front-end based technology to make HTTP-Request to server asynchronously.
AJAX Request will not reload or redirect your page. It will handle request and response in background.
Mostly using for working with database without refreshing site again and again & also for frequent requests.
Web-Socket
This is a Socket-Connection Between Client & Server. Also using TCP instead of HTTP.
This technology very famous for Real-Time Application Development like continuously changing data/graphs , Chat / Messaging Applications & Etc.
Useful links
Websocket tutorial
AJAX Tutorial

Way to send information between two web browser clients?

I've done a LOT of research on my following problem, but it all ends up pointing to Websockets which I can't use because my host doesn't allow command line or ssh. I've also tried the postMessage(data, targetDomain) technique, but that's not entirely where I want to go either.
Here's what I want to do:
Execute javascript from one person to the other users that are on that page without reloading it or anything.
Example: A user clicks a button on the page and it sends a javascript code that automatically executes a function on every other listeners page. Kind of like a chat feature you could say, but less dense maybe?
This is my first time exploring this field so I'm a bit confused over the concepts. I've read multiple documentations of programs that offer socket like usage, but not really any luck here. (Like I said, I can't really use sockets because of my host).
Any help is appreciated, thanks!
If you are on a hosting platform that doesn't allow WebSockets then you can outsource the realtime communications layer to a hosted realtime service (I work for Pusher which is one such service).
Hosted services tend to have a free tier so depending on what you are trying to achieve that may be enough. If not, then contact them, explain your app and your situation and they may be able to help.
If you want to use a self hosted solution then you may need to go with something HTTP-based. But, again, if you are on a small shared hosting platform this may be quite resource intensive and your hosting provide may tell you off.
If updates aren't all that frequent they you could use standard AJAX polling at reasonably long intervals. It sounds like you'd like to avoid this though.
Personally, I'd recommend using a hosted service as what you are trying to achieve is inherently difficult with PHP as it was built with request/response in mind.
Related: Realtime websocket-like behavior possible on Heroku?

PHP Socket Server/Client

I have read a few good articles about coding a socket server but thought I would ask here to see if there is any further knowledge/ideas about what I actually need.
I run multiple websites for clients all running off the same server, connecting to the same DB etc. Each client website has a form where users can submit their details for services we offer. These users are spread out across the world but what I am wanting to build is a monitoring system where my interface displays the users IP address, client website they are on, the page they are on etc. From the IP I will do a country/state look up (I know its not 100% accurate but close enough is good).
I would like the visiting site to send a packet to the socket server which in turns sends the output information to my screen in real time (after I perform some actions). I guess you could say I am building a mini NOC to monitor website activity. I would also like the output information to be most recent activity at top of screen but also show a scroll bar to view all activity.
Are sockets the best mechanism for this system? Any other suggestions or tutorials on how to achieve the outcome?
Many thanks.
Before you begin down this road, have you checked out the realtime part of Google Analytics? It does most of what you are looking for.
When someone is visiting your site, you aren't typically going to have a persistent connection. I'd suggest that rather than creating one, parse your server logs or store user information in a database, and query for the last x minutes of visitors.
For updating the viewing page that you are on in realtime, Web Sockets are best for this if you need very fast response time, but are currently quite the hassle to do in PHP. In addition, browser support isn't very wide. If you insist on using PHP, I'd recommend polling over AJAX. Otherwise, look into using Node.JS with Socket.IO. Socket.IO wraps up a lot of similar methods to web sockets to get the same effect with little effort. Still use PHP for your application... just use the Node.JS/Socket.IO part for your monitor interface.
Finally, I'd suggest questioning again why you might want this. You can spend a lot of time on a project like this, and the truth is that your analytics data over time is far more valuable than a snapshot when you are looking at it.

Partner-website-hosted Web App without exposing source code - Possible?

Apologize if this particular problem has been answered already (a search didn't turn anything directly relevant up).
We are developers of a web app that is used to provide community commenting and "social" to our partners websites. Our app uses Javascript and HTML on the front end, PHP and mySQL on the back.
Currently we are running everything through our own servers, which is getting very expensive.
We would like to ask our partners if we can host the app through their servers, with them getting a discount to our monthly charge due to the bandwidth/cpu load they would help us share.
My question is, is there a way to host our app through our partner's web servers in such a way that we can offload most of the CPU time and bandwidth without exposing our source code?
I would greatly appreciate any ideas/help!!
Thank you very much all!
If you also serve static or rarely changing content your clients could run a caching reverse proxy to remove some load from your servers without giving them any source code at all. But you need to implement caching headers for this to work properly.
You may want to look into nginx.
On second thought: Did you try to compile your scripts using facebooks Hip-Hop for PHP? First of all the script should perform way better, second of all, if you still had to outsource the hosting, you deploy a compiled program, no source code involved.
If you put the code on their server they can find out. So that won't be 100% working. Though you can make it difficult but it's still not great.
Most doable solution might be to separate parts of the application and share them. So: You give away a process (so source and other needed data) but it's only part of the total. That way no partner has your total solution but you do outsource the parts.

Best way to show notifications and messages LIVE on a webpage?

I am interested in a really good way of doing instant messaging like meebo and facebook and myspace all have, also for notifications on a page. Example on my site now, when a user receives a new mail message, photo comment, profile comment, friend request, some other things, they will receive a notification message and link on the page they are on using jQuery and AJAX.
I believe on a large scale that this is not the best way to do it though as my jquery code will have to basically run a PHP script to query the mysql table every 30 seconds or so for every user who has the page open with this script running. I would like to eventually add in some sort of instant messaging like facebook has in a chat bar at the bottom of the screen if I can find out an efficient way of doing it.
I have heard the terms Comet and HTML5 WebSockets but I am not sure if these are the solutions I should be looking at for such a feature? I would like to keep bandwidth at a minimum and running a query every few seconds does not seem very efficient on a high traffic site.
If you know some good solutions, please explain how they work a little bit please
You're looking for a "comet" server. Since you're talking PHP, your best solution would be a SaaS solution, such as WebSync On-Demand, since PHP has issues with scaling when it comes to large numbers of concurrent connections being held open. See also this question.
I think Facebook uses XMPP for their chat, so have a look at it.
XMPP is
an open technology for real-time communication, which powers a wide range of applications including instant messaging, presence, multi-party chat, voice and video calls, collaboration, lightweight middleware, content syndication, and generalized routing of XML data.
You'd install a separate server to handle messaging and in your app you'd implement a client with one of the available libraries.
Could you have a shared message queue that ajax can query? Maybe its a php script that stores a majority of the message in memcached (and possibly write to db in case of failure).
Not sure, but curious to hear other solutions ...
The basic problem is that you need to implement a Comet server (ie implement Server Push). The standard web hosts and apache don't readily allow you to do this easily. Another hosted solution (apart from Web Sync) is Goldfish Server. Currently free.

Categories