In this tutorial, I will explain how to do a private one-on-one chat. Lot of tutorials out there explain how to do single user & client chat. But with this, you can create a multi one-to-one chat. Still confused? Let me give you an example;Imagine you are a company wanting to sell chat features to your clients.
For this, you will need to work on three ends viz: Server, Admin & client.
socket.io
, initialize DB connection and start listening on any port you wish to usevar socket = io("your-socket-io-server-url");
var socket = io("your-socket-io-server-url");
Everytime a client connects to the socket, it is issued a new socket ID and this ID changes everything the user reloads the page. So, the logic here is to save the socket ID and associate it with some ID that doesn't change (or at least until the user clears browser data).
Consider every admin-client as a chat node. Each chat node has one admin and multiple clients and their own room IDs. Don't get confused about this room ID with Socket IO's rooms.
Pause here and try it yourselves since you get the logic. I strongly believe in self-learning... I mean, a teacher tells you how to do something but when you learn it yourselves, you will know how "NOT" to do it and eventually do it right!
Continue ...
Follow the below in same exact sequence
onClient
event and pass room ID , username , admin username
onClient
and save all values into the database along with the socketid
chatMsgs
privately using io.to(client_socket_id).emit("chatMsgs", chatMessages)
event and pass messages object
chatMsgs
and populate the chat box body with the messages receivedadminOnline
event to get admin socket which is supplied by the admin and save it in global variable or local storage adminOnline
event when the admin emits adminIsOnline
and supply the socket idclientResponse
event and supply it message , admin socketid , roomidclientResponse
event, save the message in database , look for the socket id of the admin in the database and send the same message privately to that socket.message
event to get the message sent by the admin via chat server and update chat UI. onAdmin
event and pass a security token (this is usually the JWT token received while login).onAdmin
event and get a token that was sent by the admin. This token should authorize the server to save the user details and socket.
Query the database to get all users connected (previous & current). Emit chatUsers
event and supply the users receivedclientOnline
to get the users that are online. This is where you need to update the local usesr variable and update UI var userTypingTimeout = null
socket.on('userIsTyping', data => {
if (data === currentRoomId) {
$('#userFeedback').show().text("User is typing ...")
clearInterval(userTypingTimeout)
userTypingTimeout = setTimeout(function() {
$('#userFeedback').hide()
}, 3000)
}})
I've left out some pieces of the tutorial deliberately. The idea is to show you a direction on how to attain private chat. Figure out things like User Disconnected
, User reconnected
, End chat
etc. There are
more refinements you can do such as initiate chat connection only when the user enters details such as name , email , phone etc.
Thank you and feel free to comment your thoughts
Do you want to access your webcamera remotely? Then this article is for you.
Calendar Picker / Appointment Setter JS CSS Library. Inspired by Calendly.
Create a local file sharing server with realtime sync feature using Socket IO.
Most beautiful Navbars designed with tailwind css. Fully responsive templates.
Most beautiful dashboards designed with bootstrap 5. Inspired mostly from dribble and other sources.
Most commonly used HTML email templates. Welcome email, sign up, password reset etc.
Checkout our most downloaded payment page ui designed with bootstrap.
Detect user's inactivity and auto logout after certain timeout using various JS features.
Keep the user engaged and gain recurring website traffic. Includes templates.
How to get a user's location using Javascript and other techniques available today.
This website uses cookies and similar technologies, to enhance your browsing experience and provide personalized recommendations. By continuing to use our website, you agree to our Privacy policy.