Replies: 1 comment
-
Hi! Did you check our React guide: https://socket.io/how-to/use-with-react? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Like the title says, I wanna know if I can establish a socket.io connection in my frontend and then make the same instance emit messages. Then listen for those same messages within the frontend? I am working on a chat app and want to enable real-time messaging. So according to my component code below, when a user sends a message, the
handleSubmit
function is called. Inside thehandleSubmit
function I'm making an AJAX request to upload the text message to the database through the backend server. For receving messages, currently, I only have auseEffect()
hook which will executefetchChats()
. But this component currently doesn't support real-time updates. So my question is how can I setup socket.io in this component. I would want the io connection to emit a message with message data including "recipientEmail" (which is the email id of the user we're texting to). And then also constantly keep listening for a message, once a message is received it will check whetherrecipientEmail
in the message's metadata is equal toclient
(here I'm now talking from the 2nd user's perspective, whereclient
is the second user.)My current component is as follows:
I tried implementing a socket.io connection on my own (as in the code above), but it doesn't work. The message gets emitted and the result of
console.log(res)
(after emitting the message) gives something like this:which clearly states that the connection couldn't be established.
Beta Was this translation helpful? Give feedback.
All reactions