DEV Community

Cover image for How to use socket.io-client correctly in React app

How to use socket.io-client correctly in React app

bravemaster619 on December 16, 2020

Background The following is my first blog to achieve 8k+ view and 1st in Google SERP rank. However, it has some concerns and I decided t...
Collapse
 
lethanhvietctt5 profile image
Le Thanh Viet

I have learned about ReactJS and Socket recently and written an video call and chat app using ReactJS and Socket .Here is my source code: github.com/lethanhvietctt5/video-c.... I hope it can help you guys to build a video chat app.

Collapse
 
arkadiuszkozub profile image
Arkadiusz Kozub

I don't think it's good approach. It will work for very small projects, but for larger ones not so much. Mixing socket communication with React will become overwhelming as app will grow, because all of it will spread through entire project. I would separate data source from frontend components. Imho better method is to extract actions and datasource to another files and provide only api to front components (as react-redux does). It would provide flexibility. Especially when you need to access different data sources (ie. local storage + cookie + websocket + ajax). React shouldn't "care" about where data comes from, business logic or communication with server.

Collapse
 
goran7777 profile image
Goran

Elswhere in react on that way doesnt work,we can have only one instance of socket on client.

Collapse
 
christopherhbutler profile image
Christopher Harold Butler

Great article! Could you show how to implement 'user is typing indicator' with react? It seems when you keep track of a keyPress using a setTimeout you get a call to a function that sets a variable for every keyPress. I can share a code example somewhere if you want to discuss :)

Collapse
 
bravemaster619 profile image
bravemaster619

Thanks! If you want to ask me anything, please open a stackoverflow post and share the link. I'm more than happy to help there :)

Collapse
 
dtweblife profile image
Daniel Thomas

I don't understand the JWT part. It's not written in relation to context. I have my app with a callback that is receiving the jwt on login then storing in storage which is when I want to connect to the socket, but I'm not too sure how to pass that jwt to context or force it to update when the jwt is available.

Collapse
 
derockypeter profile image
Amaizu Somto Peter

Nice tutuorial. In my VS CODE console, I keep getting multiple request with polling, but it never does a a handshake with the server, what could be wrong?

Collapse
 
super_shinobi_767f9988403 profile image
super shinobi

same question here

Collapse
 
mirik999 profile image
mirik999

Good example. Thanks

Collapse
 
sajaddp profile image
Sajad DP

@bravemaster619
It is greate article, Thank you!

When the server emit it sends some values. How we can access these values in useCallback‍?

Collapse
 
greybax profile image
Aleksandr Filatov

nice! I did almost the same in my coding challenge which I've recently passed and described it in here alfilatov.com/posts/frontend-dto-c...

Collapse
 
lizardkinglk profile image
sndp

And for initialize and listen of the events of the socket, it is better to use different useEffect hooks separately.

Collapse
 
chrisnabayuaji profile image
Chrisna Bayu Aji

How to handle when client disconnected from server.

Collapse
 
bunty9 profile image
Bipin C

how do you globalize the socket instanse? ,,, so that i can call emits and listen to them in other modules of the project.

Collapse
 
whiz7z profile image
Whiz7z • Edited

I've made online Rock-Paper-Scissors game, using sockets like that. Thanks. You can check it here
[github.com/Whiz7z/rock-paper-sciss...]