Realtime Chat Application using PHP with MySQL & JavaScript Ajax

Hey friends, today in this blog you'll learn how to create a Realtime Chat Web Application using PHP with MySQL & JavaScript Ajax. Earlier I have shared a blog on how to create a Simple Chatbot using PHP with MySQL & jQuery Ajax. Our maximum viewers have requested me to create a Chat App so I decided to create one.

In this chat app, when you open it first on your browser, there is shown a signup form where you have to signup with your details like name, email, password, and image. Email and image field is fully validated which means you've to enter a valid email and an image file only. Once you signed up successfully, you'll be redirected to the user's page where you can see your full name, image, status, and logout button to the top, and users, like you, appear on the bottom if someone has signed up. On this page, you can see their image, name, status, and the last message if they sent to you. You have to click on the particular user or you can also search any existing user with their name then you'll be redirected to the chat page and there you can see the image, name, status of that user who is going to chat.


Once you send a message to another user then immediately that message appears in your chat box and another user chatbox too which you've sent the message. On the message receiver chatbox, this user received the message with the sender image. Remember chatbox will be automatically scrolled to the bottom once the chatbox starts scrolling. You can logout from the chat application at any time and once you logout, immediately all other users will know that you've been log out or offline.

Once you logout, you can again login and with your email and password that you used to when signing up for the form. If you entered the correct credentials then you'll be redirected to the user's page and all other users will immediately know that you've logged on and now active.

Video Tutorial of Realtime Chat Application using PHP



In the video, you've seen the demo of the chat app and the codes or concepts behind creating a chat app. As I already told you I used PHP with MySQL and pure JavaScript Ajax to create this chat app so you don't need to reload the page to see changes in the chat app. I know if you're a beginner and you don't have enough knowledge about PHP then definitely you've to difficult to understand the codes.

But I tried to explain each JavaScript and PHP line with comments, subtitles, etc. But don't worry I have provided the source code files of this chat app so you can easily download from the given download button and try analyzing, practicing the codes to understand better. And I know, there are many other features that are missed on this chat app but for now, I think it is more than enough for practice.


You might like this:


Realtime Chat Web Application [Source Codes]

How to download source code files and what to do after downloading them?

Steps: - To download files, just click on the given green color button then you'll be redirected to the timer page, just wait for the timer. Once the timer completed then a zip file will be automatically downloaded. Once it downloaded, just extract the zip file then you'll see the folder name with ChatApp. Copy this folder and paste it inside htdocs folder of your XAMPP then start your apache and MySQL from the XAMMP control panel. After completing these steps, go to your browser and open this URL localhost/phpmyadmin then create a new database name with chatapp. After creating the database, there you can see an import option, just click on that and import the SQL file which is in the ChatApp folder. Everything is done now, just open this URL localhost/chatapp that's it. Your chat application is ready to chat.

If you want to upload this chat application to an online server for free and don't know how? Then please watch this video How to upload Chat Application to an Online Server for Free? alert-info




99 Comments

We welcome relevant and respectful comments. Off-topic or spam comments may be removed.

  1. Giving a Eror massage when giving image


    Warning: move_uploaded_file(images/1613574012IMG-5f8954caa02539.76436861.jpg): failed to open stream: No such file or directory in G:\aiman study\htdocs\new team\register\ChatApp\php\signup.php on line 27

    Warning: move_uploaded_file(): Unable to move 'G:\aiman study\tmp\phpBCBB.tmp' to 'images/1613574012IMG-5f8954caa02539.76436861.jpg' in G:\aiman study\htdocs\new team\register\ChatApp\php\signup.php on line 27

    ReplyDelete
    Replies
    1. Please try again. Maybe you deleted images folder which is inside ChatApp>php

      Delete
  2. Replies

    1. Notice: Undefined index: incoming_id in C:\xampp\htdocs\ChatApp\php\get-chat.php on line 6

      Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\ChatApp\php\get-chat.php on line 12
      No messages are available. Once you send message they will appear here.

      Delete

  3. Notice: Undefined index: incoming_id in C:\xampp\htdocs\ChatApp\php\get-chat.php on line 6

    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\xampp\htdocs\ChatApp\php\get-chat.php on line 12
    No messages are available. Once you send message they will appear here.

    ReplyDelete
    Replies
    1. Codes now updated. Please download files again from the same link.

      Delete
    2. I just downloaded the code, but it is saying the same thing

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Codes now updated. Please download files again from the same link.

      Delete

  5. Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\ChatApp\php\data.php on line 14


    please help

    ReplyDelete
    Replies
    1. Codes now updated. Please download files again from the same link.

      Delete
    2. if you find this error again just replace line 14 in data.php file with ($outgoing_id == isset($row2['outgoing_msg_id'])) ? $you = "You: " : $you = "";
      BTW CodingNepal your work is amazing.

      Delete
    3. Now I have You: in every message in second row, don't know why man

      Delete

  6. Warning: Undefined array key "incoming_id" in E:\xampp\htdocs\ChatApp\php\get-chat.php on line 6

    ReplyDelete
    Replies
    1. Codes now updated. Please download files again from the same link.

      Delete

  7. Warning: Undefined array key "incoming_id" in E:\xampp\htdocs\ChatApp\php\get-chat.php on line 6

    ReplyDelete
    Replies
    1. Codes now updated. Please download files again from the same link.

      Delete
  8. Giving error' message When open file chat.php?user_id=....

    ⏬⏬⏬⏬⏬⏬⏬

    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /storage/emulated/0/htdocs/chatapp/chatapp/php/get-chat.php on line 12

    ReplyDelete
    Replies
    1. Codes now updated. Please download files again from the same link.

      Delete
  9. Hey, when I go to open the conversation, the messages do not appear and I appear to say that there are errors on line 6 and 12 (get-chat.php) and a fatal error. How can I solve?

    ReplyDelete
    Replies
    1. Codes now updated. Please download files again from the same link.

      Delete
  10. This work is really fire! Thank you so much. CodingNepal.

    ReplyDelete

  11. Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\ChatApp\php\signup.php on line 11
    Something went wrong. Please try again!

    ReplyDelete
    Replies
    1. Did you follow every steps that I mentioned above?

      Delete
  12. There is no images inside php/images
    Sorry but it's true

    ReplyDelete
    Replies
    1. It is...I created a folder with images name inside php folder to save users uploaded images...please check again

      Delete
  13. Hi codingNepal can You include the sound .mp3 for the notification in your chatapp ???

    ReplyDelete
  14. pls vscode dont allow me to code in php pls help me!!!!!!!!!!!!!!!!!!!

    ReplyDelete
  15. Hi, can you put a sound for the notification ?

    ReplyDelete
  16. Brother, I have a problem! All details are perfect, but the connection was not a success.

    The Error: "Warning:mysqli_connect(): (HY000/1045): ProxySQL Error: Access denied for user 'id16130301_tawfik'@'2a02:4780:bad:f00d::3' (using password: YES)"

    Please help me. How can I solve this?

    ReplyDelete
    Replies
    1. This error you faced because you've written dbname or dbpass or dbuser name incorrectly in config.php. But if you were written all things correctly then delete the current database and create another one because this is a free hosting and sometimes you get an error even if you did everything perfectly.

      Delete

  17. Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\ChatApp\php\data.php on line 14

    Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\ChatApp\php\data.php on line 14

    Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\ChatApp\php\data.php on line 14

    Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\ChatApp\php\data.php on line 14

    Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\ChatApp\php\data.php on line 14

    Its showing this i wanted the reference code but its not working idk why its showing this i ve downloaded it today 2 / 22 /2021 ( can you help me please )

    ReplyDelete
    Replies
    1. I think you changed the PHP codes so you're facing these errors.

      Delete
  18. Why is this showing something like XAMP / some folder
    can you help me idk why i am getting this

    ReplyDelete
    Replies
    1. You meant when you uploaded it to an online server, there you're getting this right?

      Delete
  19. Sir please add password change and profile change option.

    ReplyDelete
  20. Sir please add password reset and profile pic reset option in this chat.

    This chat very nice

    ReplyDelete
  21. Why is show me like this: Trying to access array offset on value of type null in C:\xamp\htdocs\ChatApp\php\data.php on line 14

    ReplyDelete
    Replies
    1. Please contact us here - codingnepalweb@gmail.com

      Delete

  22. Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /storage/ssd4/625/16245625/public_html/php/signup.php on line 11
    Something went wrong. Please try again!

    ReplyDelete
    Replies
    1. Please contact us here - codingnepalweb@gmail.com

      Delete
    2. Actually I repoted it a littli late.
      Now i dont get this error but now I have another error i have reported it in these comments

      Delete
  23. Hello there;
    I downloaded the source codes. I edited the config.php file with my own information. But do you know the reason I got this error? Note: I get an error like this when a second user is online. The online user does not appear. You can reach for a reply at oglcn007@gmail.com. Thx


    Fatal error: Uncaught TypeError: mysqli_fetch_assoc(): Argument #1 ($result) must be of type mysqli_result, bool given in C:\xampp\htdocs\chat\php\data.php:7 Stack trace: #0 C:\xampp\htdocs\chat\php\data.php(7): mysqli_fetch_assoc(false) #1 C:\xampp\htdocs\chat\php\users.php(11): include_once('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\chat\php\data.php on line 7

    ReplyDelete
    Replies
    1. Please share the screenshot of your problems here - codingnepalweb@gmail.com and also explain when you're getting this error?

      Delete
  24. how do i sort user in the user.php by incoming_msg_id

    ReplyDelete
  25. Database connection errorNo such file or directoryAll input fields are required!
    Please help CodingNepal

    ReplyDelete
  26. Database connection errorNo such file or directory
    Please answer fast what to do

    ReplyDelete
  27. hi can you make a tuto about how you can send a message in chat? and add reset pass thanks, new subscriber

    ReplyDelete
  28. Hi sir I want to add your code in our project but this case I'm already login.so how can set plz explain.

    ReplyDelete
  29. i want to show user list according to the last received or outgoing message (like fb) and also i want seen or unseen msg tab (read or unread also okay )
    i am waiting forr the update, can you plz tell when exactly we get next update ? this is cool

    ReplyDelete
  30. I really like your all tutorial . Your work is awesome. Please put the demo of each tutorial with download. Thank you very much

    ReplyDelete
  31. Can I use this code on my website.

    ReplyDelete
  32. Hello Coding Nepal, thanks for the code, it is really useful.

    I have a problem to register, it does not show what the error is, but I believe the problem is in the image upload.

    Because I put the image in the images folder, and entered the data manually in the database and it worked.

    Do you know what the possible error is?

    ReplyDelete
  33. hi, thanks for script.I install the script, everything ok. but not registering.when open the console this alert seen,[DOM] Input elements should have autocomplete attributes (suggested: "current-password"): (More info: https://goo.gl/9p2vKq) input type="password" name="password" placeholder="Enter new password" required=""

    ReplyDelete
  34. I take console alert
    [DOM] Input elements should have autocomplete attributes (suggested: "current-password"): (More info: https://goo.gl/9p2vKq) input type="password" name="password" placeholder="Enter new password" required=""

    ReplyDelete
  35. You know? You make me surprise every time! Thank you!

    ReplyDelete
  36. how to open it i am not getting

    ReplyDelete
  37. You have a error when render user image account who send a message. receiver user show samself image but not sender. You need a change SQL query on file get-chat.php from ... users.unique_id = messages.outgoing_msg_id ... to ... users.unique_id = messages.incoming_msg_id ...

    ReplyDelete
  38. how can i add this chat box in xml blogger website.

    ReplyDelete
  39. It Works perfect in my localhost thanks bro

    ReplyDelete
    Replies
    1. What are softwares we should download to run this sir?

      Delete
  40. Hello! Can you add file sending to chat? Please

    ReplyDelete
  41. Hello good evening, please can you do a tutorial on how to make the chat application mulitilingual.

    ReplyDelete
  42. Amazing man!! You do a lot of work for the society.

    ReplyDelete
  43. I have successfully installed your chatapp.
    Except when I want to create a user or log in. It gives me no error, but no page loads. However, to verify the connection to the database, I created a user from the database, and when I want to create an account, it tells me "Email already exists". I therefore conclude that the connection to the database is successful.However, I don't know where the error came from.

    ReplyDelete
  44. Not Found
    The requested URL was not found on this server.

    Apache/2.4.46 (Win64) OpenSSL/1.1.1j PHP/8.0.3 Server at localhost Port 80

    ReplyDelete
  45. Bonjours

    je voudrais savoir comment je peux faire pour retirer l'obligations de mettre une photo

    merci

    ReplyDelete
  46. Hello
    I would like to know how to remove the obligation to import an image

    thank you

    ReplyDelete
  47. Something went wrong. Please try again! this is the error am getting

    ReplyDelete
  48. encrypt the message from the database and retrieve them in the chat

    ReplyDelete
  49. I can't Connect SQL.
    "
    Database connection errorAccess denied for user 'root'@'localhost' (using password: NO)All input fields are required!

    "

    ReplyDelete
  50. "403 access denied "Bhai Please Bro help Me on this.

    ReplyDelete
  51. Bro it works fine but its not working after

    ReplyDelete
  52. Its working fine on local host but not working on web it shows "403 access denied " can you help me with that

    ReplyDelete
  53. I am getting this error

    Warning: move_uploaded_file(images/1618997904lesson.jpg): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/ffyypp/ChatApp - CodingNepal/php/signup.php on line 28

    Warning: move_uploaded_file(): Unable to move '/Applications/XAMPP/xamppfiles/temp/phpmTMVIx' to 'images/1618997904lesson.jpg' in /Applications/XAMPP/xamppfiles/htdocs/ffyypp/ChatApp - CodingNepal/php/signup.php on line 28

    ReplyDelete
  54. Tell me sir,what r the softwares should download sir ?for this project

    ReplyDelete
  55. Hello coding Nepal, I have uploaded this project on server on infinityfree server.

    Whenever I open chat it shows a popup "website configuration prevents to access this page"

    Here is my website url
    http://whatschat.epizy.com/

    Please help me

    ReplyDelete
  56. I have uploaded this project on infinityfree server.
    Whenever I open a chat it redirect me on error page "website configuration prevents to access this page"

    My website url http://whatschat.epizy.com/

    Help me out

    ReplyDelete

Post a Comment

We welcome relevant and respectful comments. Off-topic or spam comments may be removed.

Previous Post Next Post
CodingNepal
Subscribe our YouTube Channel