Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a chat application [closed]

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.

like image 833
Isamtron Avatar asked Sep 10 '10 04:09

Isamtron


People also ask

How do you explain chat application?

A chat application makes it easy to communicate with people anywhere in the world by sending and receiving messages in real time. With a chat app, users are able to receive the same engaging and lively interactions through custom messaging features, just as they would in person.

Is Websocket good for chat application?

WebSockets are a great fit for applications like chats or simple games. Chat sessions are usually long-lived, with the client receiving messages from other participants over a long period of time. Chat sessions are also bidirectional – clients want to send chat messages, and see chat messages from others.

How do you make a chat app without coding?

To build a chat application for android with AppsGeyser, simply open the “Chat” App Template on AppsGeyser.com, or click the “Create App Now” button on this page, customize your app, give the app a name, upload an icon, and publish on Google Play.


2 Answers

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.

like image 94
Josh K Avatar answered Oct 17 '22 23:10

Josh K


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.

like image 2
NullUserException Avatar answered Oct 18 '22 01:10

NullUserException