8

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.

Thanasis1101
  • 1,614
  • 4
  • 17
  • 28
  • 2
    websockets is what you're looking for. – Matt Jun 14 '17 at 17:58
  • I think facebook doesn't use websockets (see [here](https://www.quora.com/Does-Facebook-use-WebSockets-for-any-of-their-applications-Are-they-really-useful-at-that-scale-especially-since-they-impose-a-stateful-architecture)). But if it is a better approach, I would like to know what are the first steps I should take and what I should know. – Thanasis1101 Jun 14 '17 at 18:12
  • 2
    Who cares what facebook does or what some random person on the internet says about facebook does from two years ago! websockets is the technology that is used for exactly what you are trying to do. If you open up your console with F12, you can see if they are using websockets under the network tab and you can filter by WS. – Matt Jun 14 '17 at 18:45

5 Answers5

3

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

Trapenok Victor
  • 349
  • 2
  • 6
2

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.

Community
  • 1
  • 1
Harish Kulkarni
  • 1,481
  • 11
  • 18
2

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

Supun Kavinda
  • 1,355
  • 14
  • 13
0

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)

Matt
  • 5,315
  • 1
  • 30
  • 57
  • 1
    Thanks for your answer. Ok, the answer is websockets, but you do not show any guidelines as I asked on my question, because I am a little lost with the scattered information that I find online. What I want to know is with what I should start (what I need to install on my computer, what I should learn) and how I can implement these with php and javascript. If you know any good tutorial or article, or you can write some steps (including how to use the projects from the links you sent me), that would be really appreciated. – Thanasis1101 Jun 14 '17 at 19:10
  • I gave you a client side library and a server side library. (Even though library requests are specifically off-limit for stackoverflow. https://stackoverflow.com/help/on-topic) Websockets are extremely complicated and if your expectation is to be taught how to use them through stackoverflow you're going to be disappointed! You might try google searches like "autobahnjs getting started" – Matt Jun 14 '17 at 19:14
  • 1
    Ok, thank you for your time. I was just searching for something that could guide me through this extremely complicated concept of websockets. – Thanasis1101 Jun 14 '17 at 19:22
0

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 .