Creating a real-time chat with php and javascript
Asked Answered
K

5

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.

Kennethkennett answered 14/6, 2017 at 17:45 Comment(3)
websockets is what you're looking for.Exsanguinate
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.Kennethkennett
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.Exsanguinate
W
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

Woodworth answered 13/7, 2017 at 13:44 Comment(0)
F
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.

Facetious answered 9/5, 2018 at 10:34 Comment(2)
This uses an interval to keep chat updated, it is not realtime.Loram
Yeah @VaelVictus , It is not completely real time, Just checks if any new entry in DB at interval times. I wrote it long back will update my answer soon.Facetious
K
2

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

Kinescope answered 14/4, 2019 at 21:39 Comment(0)
E
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)

Exsanguinate answered 14/6, 2017 at 18:47 Comment(3)
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.Kennethkennett
I gave you a client side library and a server side library. (Even though library requests are specifically off-limit for stackoverflow. 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"Exsanguinate
Ok, thank you for your time. I was just searching for something that could guide me through this extremely complicated concept of websockets.Kennethkennett
C
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 .

Claudieclaudina answered 21/9, 2020 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.