Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I_Helmet
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
21_22-J 62
I_Helmet
Commits
ebe36b8d
Commit
ebe36b8d
authored
Jul 04, 2022
by
Balasuriya D.A.M.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Realtime Typing Functionality
parent
485734fe
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
1 deletion
+31
-1
IT18021080/Telemedicine-Chat-App/backend/server.js
IT18021080/Telemedicine-Chat-App/backend/server.js
+3
-0
IT18021080/Telemedicine-Chat-App/frontend/src/components/SingleChat.js
...lemedicine-Chat-App/frontend/src/components/SingleChat.js
+28
-1
No files found.
IT18021080/Telemedicine-Chat-App/backend/server.js
View file @
ebe36b8d
...
@@ -58,6 +58,9 @@ io.on("connection", (socket) => {
...
@@ -58,6 +58,9 @@ io.on("connection", (socket) => {
console
.
log
(
"
User Joined Room:
"
+
room
);
console
.
log
(
"
User Joined Room:
"
+
room
);
});
});
socket
.
on
(
"
typing
"
,
(
room
)
=>
socket
.
in
(
room
).
emit
(
"
typing
"
));
socket
.
on
(
"
stop typing
"
,
(
room
)
=>
socket
.
in
(
room
).
emit
(
"
stop typing
"
));
socket
.
on
(
"
new message
"
,
(
newMessageRecieved
)
=>
{
socket
.
on
(
"
new message
"
,
(
newMessageRecieved
)
=>
{
var
chat
=
newMessageRecieved
.
chat
;
var
chat
=
newMessageRecieved
.
chat
;
...
...
IT18021080/Telemedicine-Chat-App/frontend/src/components/SingleChat.js
View file @
ebe36b8d
...
@@ -21,6 +21,8 @@ const SingleChat = ({ fetchAgain, setFetchAgain }) => {
...
@@ -21,6 +21,8 @@ const SingleChat = ({ fetchAgain, setFetchAgain }) => {
const
[
loading
,
setLoading
]
=
useState
(
false
);
const
[
loading
,
setLoading
]
=
useState
(
false
);
const
[
newMessage
,
setNewMessage
]
=
useState
();
const
[
newMessage
,
setNewMessage
]
=
useState
();
const
[
socketConnected
,
setSocketConnected
]
=
useState
(
false
);
const
[
socketConnected
,
setSocketConnected
]
=
useState
(
false
);
const
[
typing
,
setTyping
]
=
useState
(
false
);
const
[
isTyping
,
setIsTyping
]
=
useState
(
false
);
const
toast
=
useToast
();
const
toast
=
useToast
();
const
{
user
,
selectedChat
,
setSelectedChat
}
=
ChatState
();
const
{
user
,
selectedChat
,
setSelectedChat
}
=
ChatState
();
...
@@ -67,7 +69,10 @@ const SingleChat = ({ fetchAgain, setFetchAgain }) => {
...
@@ -67,7 +69,10 @@ const SingleChat = ({ fetchAgain, setFetchAgain }) => {
useEffect
(()
=>
{
useEffect
(()
=>
{
socket
=
io
(
ENDPOINT
);
socket
=
io
(
ENDPOINT
);
socket
.
emit
(
"
setup
"
,
user
);
socket
.
emit
(
"
setup
"
,
user
);
socket
.
on
(
"
connection
"
,
()
=>
setSocketConnected
(
true
));
socket
.
on
(
"
connected
"
,
()
=>
setSocketConnected
(
true
));
socket
.
on
(
"
typing
"
,
()
=>
setIsTyping
(
true
));
socket
.
on
(
"
stop typing
"
,
()
=>
setIsTyping
(
false
));
},
[]);
},
[]);
useEffect
(()
=>
{
useEffect
(()
=>
{
...
@@ -91,6 +96,8 @@ const SingleChat = ({ fetchAgain, setFetchAgain }) => {
...
@@ -91,6 +96,8 @@ const SingleChat = ({ fetchAgain, setFetchAgain }) => {
const
sendMessage
=
async
(
event
)
=>
{
const
sendMessage
=
async
(
event
)
=>
{
if
(
event
.
key
===
"
Enter
"
&&
newMessage
)
{
if
(
event
.
key
===
"
Enter
"
&&
newMessage
)
{
//after sending the message should stop the typing
socket
.
emit
(
"
stop typing
"
,
selectedChat
.
_id
);
try
{
try
{
const
config
=
{
const
config
=
{
headers
:
{
headers
:
{
...
@@ -133,6 +140,24 @@ const SingleChat = ({ fetchAgain, setFetchAgain }) => {
...
@@ -133,6 +140,24 @@ const SingleChat = ({ fetchAgain, setFetchAgain }) => {
setNewMessage
(
e
.
target
.
value
);
setNewMessage
(
e
.
target
.
value
);
//Typing Indicator Logic
//Typing Indicator Logic
if
(
!
socketConnected
)
return
;
if
(
!
typing
)
{
setTyping
(
true
);
socket
.
emit
(
"
typing
"
,
selectedChat
.
_id
);
}
//debouncing function if the user is not typing
let
lastTypingTime
=
new
Date
().
getTime
();
var
timerLength
=
3000
;
setTimeout
(()
=>
{
var
timeNow
=
new
Date
().
getTime
();
var
timeDiff
=
timeNow
-
lastTypingTime
;
if
(
timeDiff
>=
timerLength
&&
typing
)
{
socket
.
emit
(
"
stop typing
"
,
selectedChat
.
_id
);
setTyping
(
false
);
}
},
timerLength
);
};
};
return
(
return
(
...
@@ -196,6 +221,8 @@ const SingleChat = ({ fetchAgain, setFetchAgain }) => {
...
@@ -196,6 +221,8 @@ const SingleChat = ({ fetchAgain, setFetchAgain }) => {
)}
)}
<
FormControl
onKeyDown
=
{
sendMessage
}
isRequired
mt
=
{
3
}
>
<
FormControl
onKeyDown
=
{
sendMessage
}
isRequired
mt
=
{
3
}
>
{
isTyping
?
<
div
>
Loading
...
<
/div> :
(
<></
>
)}
<
Input
<
Input
variant
=
"
filled
"
variant
=
"
filled
"
bg
=
"
E0E0E0
"
bg
=
"
E0E0E0
"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment