Thanasis1101
Thanasis1101

Reputation: 1680

Creating a real-time chat with php and javascript

My goal is to create a real-time chat similar to the Facebook chat, from scratch. I want to store all the messages on a database table (MySQL) and every time a new message is sent by a user, if the receiver is connected then a request will be sent to the receiver's browser and the message will appear on the chat window.

I don't want to have the client to check if a new message for the user was sent, but I want the server to send the request to the client's browser.

I know that this can be achieved using the Comet technique (I saw this stackoverflow question) but I am not able to find a good guide on how to implement this for this certain problem.

I want to use php and javascript and as less extra software or frameworks as possible.

I use WAMPServer and I have Windows.

If you know a good guide or tutorial or can provide any guidelines on how I could achieve what I want, it would be very helpful.

Upvotes: 8

Views: 30194

Answers (5)

Veer Metri
Veer Metri

Reputation: 7

Unfortunately you can't make a real time application with PHP it self you can use a framework like Laravel in PHP and use packages like laravel-websockets and create a realtime application. laravel-websockets is really useful for creating a realtime application. laravel and the laravel websokcets with any front end you can do this

http://beyondco.de/docs/laravel-websockets

https://laravel.com/docs/

You can easy create anything with it just try to understand the fundamental concept of websokcets .

Upvotes: 0

Harish Kulkarni
Harish Kulkarni

Reputation: 1581

I have already developed an application which is not completely Real-time messaging system, but it works like realtime. Built using without any external new frameworks/API, just used known and familiar skills to develop this using: Ajax jquery, PHP, Mysql, Javascript.

Logic used is:

  1. All messages will be stored in database,
  2. When you load page all messages will be loaded from database.
  3. When you get new messages after reloading, the new messages has to be loaded/displayed without reloading whole page again right? This is done using javaScript and ajax jquery. I have set time out for EVERY 0.5 seconds to reload only new messages and display them.
  4. In my code, At first when the page loads all messages will be loaded in div tags each, Later whenever new message gets into db it will displayed into new
    div tags. its Simple and works without any external API.

To refresh new messages and throw them in to div tags .load() from ajax jquery is used, to refresh every 0.5secs Javascript is used to set timeout.

Upvotes: 2

Supun Kavinda
Supun Kavinda

Reputation: 1483

You can also use Node.JS with PHP. Creating a Real-Time Chat App with PHP and Node.js

Upvotes: 2

Trapenok Victor
Trapenok Victor

Reputation: 376

Try use for this CppComet open source comet server. There have api for php and other languages. And viwe this chat example or this

Upvotes: 3

Matt
Matt

Reputation: 5428

I don't know what your exact question is but Websockets is the answer!

https://github.com/crossbario/autobahn-js

https://github.com/voryx/Thruway

(FYI, when you see WAMP in the context of websockets they're talking about something that's not windows/apache/mysql/php)

Upvotes: 0

Related Questions