DEV Community

Cover image for Building a Real-Time Chat Application with Socket.io, Express, React.js, and Chakra UI (Vite Setup)

Building a Real-Time Chat Application with Socket.io, Express, React.js, and Chakra UI (Vite Setup)

Jagroop Singh on September 23, 2024

In this blog, we’ll build a real-time chat application using Socket.io for bi-directional communication, Express.js for the server, React.js for th...
Collapse
 
charlesr1971 profile image
Charles Robertson

This is a great starter app, but it would be really great, if you could show us how to set up an admin to multiple user chat app. So, there is one admin and he can chat with multiple users, but each user only sees his/her own messages & the response from the admin. The admin would be able to see all users in a list and then choose which user to interact with.
This would be a brilliant real world chat app tutorial, if you don’t think it’s too complicated.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Great idea !!
But I think it's good for Bootcamp but it's too complicated for covering this in blog.

Collapse
 
charlesr1971 profile image
Charles Robertson • Edited

I tried creating this app in Angular, it worked pretty well, but after a while messages started to leak to the wrong user.

As I remember I sent:

userid
threadid 
messageid 
Enter fullscreen mode Exit fullscreen mode

As admin, I could then choose which user to send a message back to.
Of course if the userid in the client, doesn’t match then the message isn’t shown. But this seems a really inefficient way of pushing data. It would be far better to only send messages from the admin to a specific user.
However, I guess this is impossible, because a websocket cannot broadcast to specific clients? Or can it, if we take advantage of the room feature? Maybe if a room has the same id as the userid, then this would work?

Thread Thread
 
jagroop2001 profile image
Jagroop Singh

@charlesr1971 , you can create room with specific id for example jobId and add only those user's to socket room which you want to.

Thread Thread
 
charlesr1971 profile image
Charles Robertson

Is it possible to push a message back to an individual client, if multiple clients are listening to the same socket?
From what I know, a message is broadcast to every client that listens and then it is up to the front end code to filter those messages out. Obviously the userid is sent back with every message and if this matches the client userid, then the user can view that message.
But if I can set each room to a specific userid then presumably it will send the response back to the correct user?
But maybe I am misunderstanding how a room works?
I did read that the socket automatically allocates an incoming message to its own room, which is represented by a random id.

Collapse
 
works profile image
Web

@jagroop2001 ,Your blog makes difficult tasks feel like a piece of cake.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks for appreciation 🥳🧡.

It would be really helpful if you will react and share this with your friends.

Collapse
 
john12 profile image
john

@works is right.

Collapse
 
john12 profile image
john

@jagroop2001 , I'm skeptical that creating a chatting application is that simple. Thank you for sharing this fantastic blog.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks @john12 🧡🧡

Collapse
 
mezieb profile image
Okoro chimezie bright

thanks a lot for sharing

Collapse
 
jagroop2001 profile image
Jagroop Singh

@mezieb You're welcome! 😊 I'm so happy my blog is useful for you!

Collapse
 
mezieb profile image
Okoro chimezie bright

yes i was integrating such functionality with socket.io,express in an angular project and you throwing more light to it help me structure things well.

Thread Thread
 
jagroop2001 profile image
Jagroop Singh

@mezieb
I’m glad to hear that! Integrating with Socket.IO and Express in an Angular project sounds exciting. If you need any more tips or have questions, feel free to ask! 😊

Collapse
 
abdul_musavir_17097 profile image
Abdul Musavir

Very Helpfull

Collapse
 
jagroop2001 profile image
Jagroop Singh

@abdul_musavir_17097 , thanks .
If you really like my efforts please react and share.
It really motivates me to write more blogs.

Collapse
 
artwork_studios profile image
Artwork Studios

Please "react" and share lol

Collapse
 
web-dev-codi profile image
Web-Dev-Codi

Beautiful post right here, Thank you for putting this together

Collapse
 
jagroop2001 profile image
Jagroop Singh

@web-dev-codi ,Thank you so much! I'm glad you liked it.

Collapse
 
ronewaonly profile image
Ronewa Maselesele

This was a sample and easy walk by through memory lane. I needed a refresher thank you .

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks @ronewaonly

Collapse
 
chesedgamesonline profile image
ChesedGamesOnline

Great, but I don't understand much... Only know NodeJS. By the way how to export console applications with it?

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thank !
However, I’m a bit confused about what you’re asking. Could you please clarify?

Collapse
 
chesedgamesonline profile image
ChesedGamesOnline

You're welcome. I mean how to set up everything like socket.io and the rest. Maybe make a video tutorial?

Collapse
 
neurabot profile image
Neurabot

Great.

Collapse
 
jagroop2001 profile image
Jagroop Singh

Thanks @neurabot

Collapse
 
lizardkinglk profile image
sndp

Nice post. Thank you. Can you tell me how/where can one deploy this application?

Collapse
 
jagroop2001 profile image
Jagroop Singh

@lizardkinglk , Sure I am also thinking about that blog.

Collapse
 
martinbaun profile image
Martin Baun

It's really helpful to see a detailed breakdown of the process. Nice!

Collapse
 
jagroop2001 profile image
Jagroop Singh

@martinbaun, Glad to hear my blog is helpful for you! 😄 Thank you!

Collapse
 
abass_koroma_c52437027be9 profile image
Abass Koroma

Thanks for your opinion I'm so happy to meet different people who I can chat with.

Collapse
 
jagroop2001 profile image
Jagroop Singh

👍

Collapse
 
avisheks profile image
Avishek sharma

This is nice bro 💯

Collapse
 
jagroop2001 profile image
Jagroop Singh

@avisheks , thanks

Collapse
 
wafi_nashwan_8a9b96b6e027 profile image
wafi nashwan

There's an awesome database library for stuff like this, on the front end! Check it

Collapse
 
jagroop2001 profile image
Jagroop Singh

@wafi_nashwan_8a9b96b6e027 ,That sounds great! I'll definitely check it out! 🚀

Collapse
 
jaikumar007 profile image
JAIKUMAR DEWANGAN

Can anyone share whole files structure in which mix frontend and backend

Collapse
 
john12 profile image
john

@jaikumar007 , I think this approach is not according to IT Industry standard.

Collapse
 
manwa_billionare_271bac2f profile image
manwa billionare

This great