Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've been thinking about creating a chat application, but don't know too much about how to create an effective one so I was hoping we could talk about the "best" and most efficient(performance) way to making one.
So the base features are probably seeing each other's messages and a chat log.
So I was considering using codeigniter and ajax to create the view and sending of each message and every time a message is sent, the message is sort in a table for the chat log. Then I thought, if every time a message is sent a query is sent, the scaling of this app would be horrible, so I was thinking of storing the log on the client's cookie or javascript object and upon leaving the chat, the log is sent and stored in the database. But then I don't know if this would work or would be extremely bad security wise.
Also, I was reading stuff on stackoverflow about chats and websockets and ran in ajax APE as an effective way to manage sending messages... But I don't really know much about websockets and how to use them etc...
Anyhow, I'm pretty sure I can make a chat application, but a good, effective, scalable one, no...
Any suggestions on the best/most effective way on making a chat application?
Some of u suggested node.js or socket.io.
Since I don't know anything about those two, which one is better?
Also I noticed that in the node.js example that he uses one server for the whole chatroom, does that mean I need a node.js server for every chat/chatroom?
Even though you are thinking about building this with PHP and AJAX, I highly, highly recommend doing something like this with Node.js if that's an option for you. It is much more suited for something like this; especially compared to AJAX which uses expensive HTTP requests in relation to Node.js A big benefit of it is that it is incredibly fast at doing many kinds of I/O and is asynchronous. Also, the Node.js process is non-blocking which helps make it even faster.
You should have a look at this example as well. The code was written by the creator of Node.js himself. Also, Node.js can interface with databases so you don't need to use text-based logs or anything.
If you're interested in WebSockets but don't want to deal with catching all of the cross browser quirks, definitely check out Socket.IO. It's a library for working with WebSockets and I've found it to be very good at dealing with all of the different browser versions out there.
Hope that helps!
node.js and socket.io seem the most appropriate for this task.
node.js allows to handle many persistent connections with low memory usage, which is quite suitable for handling all chat clients. socket.io is an abstraction over websockets / comet / long-polling that allows to pass messages between the node server and a web browser.
You can still use codeigniter or any other framework for the user interface.
If you were really going hard core big, you might want to think about replacing the database for message queues. Second Life put up their message queue evaluations for in-game messaging.
http://wiki.secondlife.com/wiki/Message_Queue_Evaluation_Notes
Although, chat logs would not likely be persisted on your server like they would with a database.
Related
I have been searching the internet for about two days now, trying to understand how web sockets work, I am relatively new at Javascript and PHP, seeing as I started about two or three months ago.
I am extremely confused about what an Http protocol is, and how to perform a 'handshake' with a server as I have been reading about. So unfortunately it is difficult for me to ask a more specific question than the one in the title.
However, even more than I would like an answer to the question in the title, I would really like a resource that will teach me about web sockets in a thorough way, such as a book or an online tutorial.
Any help is appreciated, thanks.
Sockets in general are just TCP streams that you can send data over. HTTP is built on top of them. WebSockets are build on/in parallel with HTTP, and allow for sending data back and forth to a browser in real-time. Standard sockets can do some of this, but browsers have tight security which makes it tough to use standard sockets to communicate with them.
WebSockets are useful when you need a persistent connection to the server from the browser, for real-time applications, or things like push notifications.
I don't know much about PHP implementations unfortunately, but it looks like there is a library here: http://code.google.com/p/phpwebsocket/
Personally, I'd shy away from doing things like this in PHP because PHP/Apache can be pretty heavy, and since the socket is always open while someone is viewing the page, server resources can be used up pretty quickly.
A lot of people like to use NodeJS with socket.io because then you can use JavaScript on the server and the browser, but really it is up to preference. I would look at Socket.IO and find a language that has a good client for it.
It looks like this question might be useful too. Using PHP with Socket.io
Does this one fit the bill?
http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/
If not I can find another.
I set myself a challenge today to write a jQuery chat in under half an hour. It eventually took me 40 minutes.
However, I want to improve it so the load on the server and browser isnt horrendous.
Currently the user types into a text box, presses enter, this data is sent to a .php file which updates a mySQL table and outputs all the rows on the table.
There is a set Interval on the div every two seconds to update if anyone said anything without the user pressing enter.
I just wanted to know thoughts on how to do perform this in a better way, or the most efficient way. I want to understand the best technology to use and why.
Thanks for all your input, I love stack overflow, its been invaluable to me.
Using ajax polling for a chat application with the "php back"/"javascript front" technology stack will inevitably result in a heavy server load. Http is just not designed for that kind of communication, and if you're using apache (as I assume) there is a really heavy overhead for each request.
As some of the commments indicated, you could investigate using a full stack javascript framework (i.e. Node.js on the backend).
When I had the task of accelerating an existing chat application with php backend and Javascript frontend (using periodic ajax poll), I ended up using a 3rd party server side product to handle lightweight XMPP requests. This server side product was OpenFire, but you could use eJabberd for even better performance - OpenFire is easier to set up and maintain though. The results were more than satisfactory, server load dropped significantly, and messages were delivered instantly for ~1000 online users chatting wildly away (on a less-than-average performance dedicated linux box).
It's hard to explain all the tiny details within a SO answer's scope, but luckily Ben Werdmuller # IBM went out of his way to write an awesome tutorial on this topic.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I developed a turn based game for Android, and now I want to add multiplayer gaming. I don't want to use providers like "skiller", I would like to develop my own server.
I don't have a dedicated server, but I have a php hosting with "1 and 1". Would be a good idea to use this hosting like a game server? My idea is that my Android game polls server every X seconds waiting for opponent move.
What do you think about it?
I have looked into using them for hosting and I have heard great things about their support. BUT unless your using a dedicated server that they offer (instead of the base level shared hosting service) you probably won't have the resources available to support a gaming server.
If you can handle doing the server config yourself or are good with detailed instructions on how to set one up then I highly recommend using the $20 a month linode.com base plan. I am using it for site hosting and it makes a huge difference in terms of performance and flexibility. Also I have seen some performance benchmark comparisons done between it, slicehost, rackspace, and Amazon S3 and it blew all of them (especially Amazon) out of the water. The benchmark is 2 or 3 years old, but it is still rather telling.
Linode will let you do ANYTHING you want within the bounds of the law with the server. So if you want to host an adult site they won't have a problem with it. They will probably have a problem with setting up a spam server or some shady things like that. But they are cool with everything else it seems. Plus they are probably the most affordable option out there.
I would add that you may look into the technology behind APE servers (AJAX Push Engine). Its a high efficiency chat system that works with pretty much any server-end language and front-end.
http://www.ape-project.org/
this is how I would do it, since your game is asking the server for pull request lets skip one step. Thats asking the MySQL server. So here is the way I would do it.
From what it sounds like its a html5 game meaning that you could set cookies, on the users phone. I would have a cookie or even a javascript var that then stores a JSON or ARRAY string. from this you would be able to push 1 or 0 to the PHP script that hopefully would be able to send it to the user or store it as a .txt files it is known that fetching data from mysql is slower and if you have 400 users all doing it at 5second intervals you will bring down most shared servers, They are just not strong enough to do what you want.
If your budget is small may I suggest you look this option. I cant really think of away you can do this without storing the data somewhere.
You should not listen to programming language specific arguments, most of the time they are personal preferences. What you want to do is of course possible with PHP. You can do good and bad programming in every language. For a turn based game PHP is totally sufficient, if you know how to use it. Use the programming language you are most comfortable with, and you will be just fine.
What is more important: You want your game to be successful, 1und1 is not capable of handling a way of "success", because of its localized and not scalable on demand nature. If you want to gain money or spread, you really should not fear to invest some low bucks and go for Amazons infrastructure. You will have to learn a little, but it is definitely worth it.
Most IDEs (Zend Studio, PHP Storm, ...) even have good integration already. A shared hoster or a localized storage hoster is not what you want, because if your app gets famous they simply will not be able to handle global demand. And you will have security problems. If you really expect your game to be successful, even a VPS will reach its limit almost immediately.
You can try to grow your game by using 1und1 first, and upon demand going to a better solution. But quite some games just died because the demand was much higher than expected. The worst thing about this is you have to think less euphoric but more realistic.
To sum it up: Just use the language you are most comfortable with, if you believe in your idea do not fear to invest little bucks to meet the global market, create your own protocol with as less data transfer as possible, and please poll more often than "x seconds", because a second waiting for the opponent feels like three days. And you are on your way.
Good progress! And post a link when you're done:)
You can use Firebase messaging service to implement the same. Further you have must one Web server (e.g. PHP).
Use your android code to send request to PHP file at web server.
From this web server you can send broadcast message in form of firebase Messaging which will deliver to all devices of your subscribed topic.
All android device those listen for this FCM can respond(depend on parameters set by your code).
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Using memcached as a database buffer for chat messages
A friend of mine was telling me how he used Memcache to build a PHP/Javascript realtime chat room, but I can't figure out how Memcache would help when the data is updated (possibly) every few seconds. Of course, he told me to figure it out on my own.
Can anybody provide any hint as to what advantages Memcache would serve in such an application? (I don't want to know how to do it exactly, just how Memcache would speed things up in an application where the data is constantly being updated.)
Memcache is the PHP interface to memcached (memory object caching)
It really isn't the right tool for the job. He's obviously storing the data as a key - this probably would be faster than hitting a database, but it still sucks.
If you're looking to implement some sort of real-time chat solution I would recommend looking into the following techologies. Read up on them and you'll find a solution which may suit your needs.
Flash (AS3) sockets with a chat server OR
HTML5 Web Sockets OR
COMET (Facebook uses this.)
Flash (AS3) sockets:
There are many ways to build an Actionscript 3 chat system. It's actually pretty easy if you use one of the many existing solutions out there. IE: Smartfox. You can even use the External Interface to trigger Javascript events every time a message is received.
HTML5 Web Sockets
As support for this is pretty flakey I recommend you look into degrading gracefully with Flash. Example of a library you can use: https://github.com/gimite/web-socket-js/
COMET/Long polling
This is quite an interesting approach. Effectively what you're going to be doing is "blocking" the HTTP request server-side by not returning any data until some is available EG:
while(!d = data()) {
// no data...
}
return "{data : d}"
Your browser will in most cases just sit there happily waiting for something to happen. Once data is received the browser will close the connection - this is when you process the data, and then re-open a connection (fire off a new ajax request) so that you are listening for new events!
To accomplish this I recommend using a non-blocking server such as Tornado (http://www.tornadoweb.org/)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm wondering what's the right way to code a chat application for thousands of users.
I'm just confused how will I be able to ping the server using AJAX every second or maybe less and check if there are new records in MySQL, etc with an acceptable amount of server(s) load.
I'm currently thinking about coding this using jQuery, PHP and MySQL.
Please advice. Your help would be greatly appreciated.
Client Side
For any program that needs to poll the server I would recommend WebSockets.
I wrote an extremely basic WebSocket tutorial. I also used the web-socket-js code to implement a FlashSocket that will make it work across Firefox, IE 8+, and Chrome, as well as any browser that supports WebSockets.
I don't believe that polling would be a good choice for a chat application. While it would work, the request overhead would be much higher then using a WebSocket. The tradeoff (benefit) is that more browsers support it.
Also, hitting a MySQL database to see if there are messages is going to incur a good deal of DB overhead. I would recommend using a MySQL database for chat logs, and only keep a limited number of "back" messages on hand for new connections. Then simply broadcast new messages to all connected clients. The frontend application would then take the message and append it to chat window.
Server Side
Node.js is an evented server-side JavaScript framework. While it is still young, several very interesting applications have been coded in it. The Node.js people setup a chat program (not WebSockets) the source of which has been made available. That would be a very good place to start if not wanting to write it from scratch.
There is a PHP WebSocket implementation. Depending on your requirements it could be used just fine. Having coded in both Node.js and PHP I would say I think Node.js is a better fit for this.
On the server side, you'll need a script that can tell whether or not there is new content (eg: messages) based on a timestamp (eg: last request). On the client side, you have two options:
Polling aka Periodic Refresh:
This basically means having your client poll the server in intervals to check whether or not there is new data. What you want is to keep your requests and responses as light as possible. It could also help if run the script handling these requests in a separate process.
It will be up to you to tweak the interval to one that's acceptable for both the server and the user. You can also use a Heartbeat to tell whether or not the user is still active, so you can stop polling the server if the user left the window open but is off the computer.
HTTP Streaming aka "Comet":
Using this will require some more setup; but this is basically a long-lived connection from the client to the server and the server can "push" content to the client when necessary.
Heres a simple looking websockets example: http://www.dashdashverbose.com/2010/02/nodejs-websockets-stoopid-easy-comet.html
I assume your max amount of users would depend mostly upon your connection and server software.
You could also try out IcePush - it is an ajax framework for pushing messages FROM the server TO the javascript client. This would be a perfect match for a chatclient!
If you are a java developer, you can use jwebsocket to implement the server. there are various examples on their site to start with as I'm also going through some of them.
Briefly from their website
jWebSocket is provided to you to create innovative HTML5 based streaming and communication applications on the web. HTML5 WebSockets will replace the existing XHR approaches as well as Comet services by a new flexible and ultra high speed bidirectional TCP socket communication technology. jWebSocket is an open source Java and JavaScript implementation of the HTML5 WebSocket protocol with a huge set of extensions.