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.
Related
I'm creating a social network website, and I need a IM/chat solution for it.
The conversations are only one-on-one, with the users being able to chat only with their friends (i.e. Facebook chat - no channels, no rooms ...).
The website is made in CodeIgniter(PHP) with MySQL database. Probably will be put on VPS (Linux), so I might have control on the environment. Also, it is expected to have more than 100 000 users.
So, I need some ideas about how to make the chat.
My ideas so far:
Ajax poll on every X seconds (not good solution for website this size)
Implementing Jabber/XMPP system (I have no info about this, nor is it possible)
Using 3rd party service (like Pusher.com)
creating the chat in Node.js and socket.io, and having it independent from the other part of the website
After some research and reading some answers here on SO
https://stackoverflow.com/a/11362929/928540
https://stackoverflow.com/a/5654487/928540
I really like to go with the idea of Node.js and socket.io, so I would like to hear some opinions about it, and maybe some guidance, links, help, tutorial ...
Also, it is expected to have more than 100 000 users
I think the question also is how many active users/connections.
Ajax poll on every X seconds (not good solution for website this size).
True that. This is a bad idea.
Implementing Jabber/XMPP system (I have no info about this, nor is it possible)
Something like Ejabberd with BOSH is pretty popular. I played with prosody because it is very easy to install and strophe.js as BOSH client(javascript in browser).
Using 3rd party service (like Pusher.com)
The easiest to implement(no scaling problems), but could get expensive. 5000 simultaneous connections will cost you 199 dollar per month(too expensive) which I think you might need? Then again you have about 5000 active connections so it is profitable(?).
creating the chat in Node.js and socket.io, and having it independent
from the other part of the website
Very popular solution, but right now I believe some users are experiencing memory leaks(but this might have been solved). It might be only problem with websockets transport. Also it might not really be a leak, but just high memory usage because of lazy GC. I would advice to profile before fully commiting.
Meteor might be a solution you could use. It's designed to create "live" applications and I bet building a one-on-one messaging system is a breeze.
I know it isn't PHP, but you mentioned using Node.js and I figured if you could use Node.js then you could use this.
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.
I have been in search of making live websites by using PHP. (COMET) I have been searching for a very long time already. (around a month) I have even checked some PHP chat scripts and used on my webserver, but I had some problems on most of them. (will be explained)
So far, most of the people tells the same thing: PHP isn't the best language could be used with COMET. The chat/comet examples are hacky at best.
I am asking this because, I want to have some features on my websites, like allow logged in people to chat with each other. That is where I need an alive connection to PHP pages. I am also planning on making a browser based game, and an alive connection will still be a must!
AJAX was a rare thing 2 years ago, shined with Facebook. Now pretty much everyone uses it, and it became a standard on web development. Now, the COMET based websites are growing. Youtube, Google+, Facebook, EA's Battlelog and such. I believe I should learn how to use it.
Okay, here are my questions. (Some of the information at below are the ones I gathered while searching on Google. Not sure if they're correct or not.)
Some languages like Python have special web servers designed for this job. I believe one of them is called Tornado Web Server. Developed and configured to simulate thousands of alive connections. I believe there is no such option in Appserv, but some people told NGINX can handle it with a decent configuration. Is it true? What configurations should be made? Is there any PHP web servers for this job?
Some of the most suggested things are:
a. Using APE.
b. Using Socket.IO
c. Node.js
Is there any way we can use them with PHP? Which one is the most promising? Could you give some guidance on them? Is there anything else than these?
I have used a comet chat script. Basically, you kept querying database and output the result with flush() to browser by sleeping it with usleep() in a loop. (This one became hard to understand so I will provide a quick code)
while(true)
{
// query database
// output the result
// flush the browser
// sleep for few seconds to lower cpu load
}
usleep() function basically destroyed my web server on Windows based operating systems. Is it normal to rely on usleep() on comet applications which runs on windows based OS? I mean, is there even a way to "sleep" PHP scripts? No matter what I do, CPU load goes to %100 on both WIN and UNIX servers.
Is PHP "really" that weak on this area? Should I give up with PHP and focus on other languages? If so, which language would you suggest? (That language should be promising. For example, there is no much use of AS3 after HTML5 addition, and AS3 is more likely to die soon while JS shines.)
What is WebSync? Can it be used with PHP?
Please bear in mind that I need to use COMET to make following applications:
A chat script, where logged in players will be able to chat eachother.
A browser based game. I already use JSON/AJAX and things like that when coding, but to receive opponents steps, I should pull the data, so an alive connection is needed. (Don't imagine advanced games, I am speaking about games like chess at best.)
I would appreciate if you can give me a short guidance. After all, I have been getting confused day by day since there are SO MANY random articles on internet. People even call setInterval() as COMET, so it is a complete mess.
There needs to be some clarification here. What you're looking for is not language specific per se. If you wanted to achieve the comet functionality with PHP you'd be relying on the Web Server (e.g Apache) to do the HTTP streaming. Also you need to understand that this is not supported in HTTP versions prior to HTTP/1.1. With that said, if you want to have a non-blocking event based web server, you're better off looking at Tornado and Node as suggested.
Comet is a programming technique that enables web servers to send data to the client without having any need for the client to request it This technique will produce more responsive applications than classic AJAX The user must create a request (for example by clicking on a link) or a periodic AJAX request must happen in order to get new data fro the server.
but it's create lots of traffic on your web server. If you want to build chat application in PHP use pusher which is a third party service and easy to use.
here is a link for pusher https://pusher.com/tutorials/realtime_chat_widget
the second suggestion is use ratchet for creating a chat application.
here is link for ratchet http://socketo.me/docs/hello-world
i hope it will help you
I just saw the McDonald's commercial which I have linked to below and I would like to try developing something similar for a festival. We have been talking about making a game in which the user has to use their iPhone and something like what McDonald's has done would be great.
My question is if anyone have an idea how I can send the data and what technology they might have used? I imagine making two websites. One on which the game itself is shown and one which is the controller but how would I make sure that the data is sent and handled fast enough?
I am familiar with JavaScript and PHP. I have been working a bit with flash and ActionScript and I am wondering if that might be best for the game (obviously not the controller)
You can see the McDonald's commercial on the link below. Basically, the user visits a webpage which is the controller and then they are able to play the ping-pong game on the big screen.
Any ideas and brainstorming on how to do this is welcome.
Watch the McDonald's Pick n Play commercial here.
Just use WebSockets. The problem with them is browser support and supporting older browsers / platforms.
To handle this there are various abstractions. I would personally recommend socket.io A solid abstraction that relies on node.js. Has a range of fallbacks (including COMET and Flash). Whilst your at it, you might want to looking into using node for realtime applications (it's great!)
I would create a socket server that hosts the game (socket TCP/IP connection). The purpose of the socket server application would be to listen for connections, wait for 2 players, start the game, decide who wins and sett-era (it could also have some queue of players that will play next and inform the players where they are in the queue).
Now, how do we establish a connection from the website to that socket server?
I wouldn't use WebSockets because it's not widely supported at the moment, disabled by default in FireFox and IE does not have it implemented yet (AFAIK).
Now you have to decide, do you want to do the website in Flash or HTML. You could even use hybrid of both. For example:
Hybrid:
Use the Flash application on the controller website to act as the communicator between JavaScript and the server. When Flash retrieves data from the socket server then it will make JavaScript take care of the retrieved command. E.g. move pad up. (I use that method for my online chat application and it works like a charm).
Flash only:
Use pure Flash application as the controller and communicator to the socket server.
Voila! You have something similar to the MacDonalds game.
I hope my answer is clear enough and feel free to comment if some of my statements are wrong.
In my view, This is what I would go for :
A small client app that will be downloaded when connected to the website.Considering it in mobile platform, java app preferably. Alternative is flash, which is also suitable, but if graphics is not our aim then I suppose better hold it off.
A server to provide real time connections to the users one-at-a-time. (As shown in video). This is the actual pain for all the games out there. I would go looking for one of these projects as platforms:
http://opensource.adobe.com/wiki/display/blazeds/BlazeDS
http://www.smartfoxserver.com/
http://www.cubeia.org/
http://appengine.google.com/
Besides that, ideas - I would have liked if it was multi player (there was quite a crowd), but that would have meant at least person winning every game & more of free give aways for them.
As alternative can be COMET a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it
have a look here: Comet
I am developing a non-real time browser RPG game (think Kingdom of Loathing) which would be played from within a Flash app. At first I just wanted to make the communication with server using simply URLLoader to tell PHP what I am doing, and using $_SESSION to store data needed in-between request.
I wonder if it wouldn't be better to base it on a socket connection, an app residing on a server written in Java or Python. The problem is I have never ever written such an app so I have no idea how much I'd have to "shift" my thoughts from simple responding do request (like PHP) to continuously working application. I won't hide I am also concerned about the memory and CPU usage of such Server app, when for example there would be hundreds of users connected. I've done some research.
I have tried to do some research, but thanks to my nil knowledge on the sockets subject I haven't found anything helpful. So, considering the fact I don't need real time data exchange, will it be wise to develop the server side part as socket server, not in plain ol' PHP?
Since your game isn't something that's working in realtime you probably don't need to go down the socket route, though it's certainly a viable option. The nice thing about sockets is that updates would be instant without requiring page refresh (or server poll), so you're right to at least consider it.
If you do want to do a more real-time server setup, you might consider using something like Electroserver - this abstracts out much of the setup for you so you don't have to write your own server from scratch, plus it's free up to a certain number of concurrent users if I recall correctly.
Finally, a third option you have is a modified POST approach using AMF. Look into AMFPHP, it lets you call methods on a PHP back-end directly from your flash application. A little bit faster and easier than simply using POST stuff, but not quite as seamless as a socket connection or a specifically built gaming server.
Lots of options out there, it sounds like you are aware of this and kudos for trying to come up with the best approach rather than just rolling with what you know! I hope this helps, let me know if you have any questions.
Here's a link to Electroserver - http://www.electro-server.com/