Home / jQuery / Private chatroom using socket.io, NodeJS and MySQL

Private chatroom using socket.io, NodeJS and MySQL

As I promised to write a tutorial on Private chatroom with MySQL DB in the previous post here I go. If you are not familiar with the basics of SocketIO and NodeJS please read the previous post and get back here. Assuming that you are now familiar with how chatroom works I’m jumping to the Database part.

I will be using PDO statements to process MySQL queries instead of using php mysql functions because mysql functions are deprecated in PHP 5.5 and likely to be removed in the near future. mysqli can be used in place of mysql or mysqli but PDO statements are far more powerful and easy to use. With PDO it is possible to connect to multiple database types and write the same query for all types of database.

I am going to show you how to retrieve the saved messages and feeding message to the database. Lets start by creating a table,

Database

Create a table names messages

chat.js

There is a slight change in the UI while receiving push message event from server. I have added time stamp to each message

While sending message to the socket server we need to capture the user’s name and message to save them to the database. For this I’m writing a function updateDB() with two parameters name and message.

Establish connection to the database

In the PHP side I have set up a method handling using switch case statement,

Lets write the update function

Now that we have finished writing the update function lets write a function to fetch the saved messages when a user logs in.

chat.js

As soon as the user’s username is accepted fetch all the saved messages from the database

Lets write the fetch messages function

That’s it! Run your node server and test your app.

Download

About Ashik

I am a Full Stack Developer and love to work on APIs and Apps. Hardcore lover of Ionic and Laravel <3

Check Also

Weather forecast API using jQuery, Ajax and PHP

Weather forecast API is widely used as widgets in small blogs to huge news website …

  • marcela

    I have this error: Cannot GET /

  • frank

    Hello Sir I just came across this your wonderful script titled “PRIVATE CHATROOM USING SOCKET.IO, NODEJS AND MYSQL”. its really amazing. I have installed it. everything seems to be working half way…… it cannot insert and retrieve data from database. it shows error Cannot GET when running directly at localhost:3000/. please help me fix it so that as a student, i can learn from you . Thanks

    • Hi sorry for the delayed response. You can try 127.0.0.1:3000

      • I am an African

        the problem still the same

  • I am an African

    any demo?

  • Oby Akwaeze

    Hello Sir Your Chat app is indeed the best on the internet. I love it. and its working fine for me. Now I want to add typing notification but its not working. I have added this code to

    socket.on(‘notifyUser’, function(name){
    var me = $(‘#username’).val();
    if(name != me) {
    $(‘#notifyUser’).text(name + ‘ is typing …’);
    }
    setTimeout(function(){ $(‘#notifyUser’).text(”); }, 10000);;
    });

    please what am I doing wrong. please can you help me fix that

    Thanks

    • You haven’t asdes thw error message here. From my assumption you are sending a JSON object feom server.js but in the client side you are displaying the name variable. You shoukd use name.name to get the sender’s name

      • Oby Akwaeze

        Everything Resolved Thanks. You are the best

  • Udoka Ezepue

    Get computer science/companies already made projects with chats here https://www.wakabia.com/2016/09/get-computer-science-final-year-already.html?m=0

  • Pierre Aubameyang

    Where is Nodejs code?

Do you wan't to get notified?

I will not spam your inbox. I will only send email whenever I publish a new article or to share information about technology

You have Successfully Subscribed!