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.
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.
Share Improve this question asked Jun 14, 2017 at 17:45 Thanasis1101Thanasis1101 1,6804 gold badges19 silver badges30 bronze badges 3- 2 websockets is what you're looking for. – Matt Commented Jun 14, 2017 at 17:58
- I think facebook doesn't use websockets (see here). 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 Commented Jun 14, 2017 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 Commented Jun 14, 2017 at 18:45
5 Answers
Reset to default 3Try use for this CppComet open source comet server. There have api for php and other languages. And viwe this chat example or this
You can also use Node.JS with PHP. Creating a Real-Time Chat App with PHP and Node.js
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:
- All messages will be stored in database,
- When you load page all messages will be loaded from database.
- 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.
- 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.
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)
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 .