Messenger in PHP from scratch!

Umair Jibran
3 min readJul 15, 2020

In this tutorial we will learn to create a messenger application from scratch in Core PHP (no framework). Firstly we need User’s signup and login, so we don’t broadcast every message to everyone, we could if we want to, but not in this tutorial. I used Bootstrap 5 for front-end designing, Bootstrap 5 is still in Alpha stages so when it’s stable release is out, it maybe different!

After User’s Login and Sign-up is developed we now need to work on the the main page, displaying all the messages to the logged-in user. For that we have a table in our database by the name of tbl_convos here we store all the conversation’s members, preview up to 50 characters of the last message dispatched by any user, and total number of messages conversation by those two particular users.

Home Page for user with conversation with 1 User

Now we need a separate page for conversation between two users, for that we have this page here, upon loading the page, we load all the previous messages from tbl_messages where either participants’ IDs matches either the logged-in user or the person message is being sent to. And are displayed in manner of Last Message to be shown first sorted by ID! we could sort them via date/time sent/received and it would be accurate as well, but for maximizing the performance sorting by ID is perfect for us.

Now we also need the application to be able to send new messages as well, for that we are using form, to take our message in input field of text type, we are then sending it to the database where the data is fetched by the other user, and show in way explained earlier. For sending message to the other user, we need to export that data to our tbl_messages but we also need to update our tbl_convo in order to update the preview and number of messages, for that purpose the following block of code is used.

Until now we were working on how to send message to the person we already have conversation with, but what about those, with whom we don’t have any active conversation with, for that we have a separate page from where we can choose with whom we can start conversation with, the drop-down menu in that page will show us every registered user’s name and email, for us to start chat with.

When there are no messages for the user
User selecting who to start conversation with
First message with a new user

and to start chat with the new person we do need to send message as usual as our continuous chat, but for this chat we do need to register a new row in our tbl_convo and how we would achieve that, is this simple.

INSERT INTO `tbl_convo` (`convo_id`, `msg_participant1`, `msg_participant2`, `total_messages`, `last_message`)                                 VALUES (NULL, '${userID}', '${recepientUserID}', 1, '${preview}')

We only need to register the tbl_convo with the both participants’ IDs and the message previews, since it’s the first message the total messages of the conversation will be 1.

viola! we have our personal Messenger in Core PHP.

p.s. here’s the source code on GitHub if you want.

Applaud if it was helpful, it cheers me up!

--

--