Socket IO with React and Typescript

  Рет қаралды 18,292

The Nerdy Canuck

The Nerdy Canuck

Күн бұрын

Check out how to make a wonderful SOCKET IO connection with React & Express.
Video Info
Music by Tesko / prod. THXMOS: / @prod.thxmos6970
Github: github.com/joe...
Support
Like this video? You can show your support by buying me a coffee ☕❤️: www.buymeacoff...
Socials
Github: github.com/joe...
Twitter: / thenerdycanuck
About The Channel
The knowledge that I share here is mostly from my personal experiences. I wouldn't say I'm a 20 year professional, rather I am just a DevOps / Software Engineer with a lot of experience who enjoys teaching what I know in my spare time. I believe that sharing some of my basic knowledge on this platform can help others get started. There are many ways to program something and the videos you see here are my take on the NodeJS / Typescript / and sometimes Unity world. If this channel helps you learn, or helps you think differently then my job is done 😊.
#reactjs #socketio #typescript

Пікірлер: 25
@JoaoKunha
@JoaoKunha Жыл бұрын
I stumble on the issue sockets/Component State not updating, only way to work was remove and add the eventlistener each time, but even so was not fine. I'll try to add the state to the Websocket context, like you did. Well done. Thank you
@MislavBreka
@MislavBreka Жыл бұрын
Hey! I believe the problem you were facing while disconnecting is because you send socked ids instead of client uids. Instead of Object.values you meant to use Object.keys in few lines there. Hope this helps
@CiceroRafaelSilvadeOliveira
@CiceroRafaelSilvadeOliveira 7 күн бұрын
Eu entendi porque a informação de usuários online está errada no client, é o seguinte, o dicionário de usuários conectados que temos no servidor é do tipo { [uid: string]: string } onde as chaves são os id's gerados no servidor usando o v4 e os valores são os socket id's. Ao enviar a lista de usuários do servidor para o client nós enviamos os valores do dicionário de usuários conectados, mas quando emitimos o evento 'user_disconnected' nós mandamos o uid do usuário que desconectou, ou seja, mandamos uma chave desse dicionário, só que o client não tem uma lista das chaves, ele tem uma lista dos valores desse dicionário, então quando o client tenta executar a ação 'remove_user' do SocketContext nenhum item da lista é retirado. Para corrigir isso é só trocar 'const users = Object.values(this.users);' por 'const users = Object.keys(this.users);' no arquivo socket.ts do servidor.
@wjs7881
@wjs7881 Жыл бұрын
Awesome! Thank you.
@physcript2436
@physcript2436 2 жыл бұрын
this is great thank you
@van8646
@van8646 Жыл бұрын
amazing, thank you so much
@m12652
@m12652 2 жыл бұрын
Thanks for this :)
@alexandergarzo9415
@alexandergarzo9415 Жыл бұрын
Thank you tor this tutorial! I have a question, where and how should I deploy the server? please help!
@christianangelomsulit3759
@christianangelomsulit3759 Жыл бұрын
AWS, Vercel, Linode etc..
@yukishimazu6378
@yukishimazu6378 Жыл бұрын
Awesome video! Thank you so much!! I have one question. When one of the clients disconnects, the number of Users online doesn't change in the other clients' page( 36:13 ). Can you solve the problem? I think when users: string[] is updated, react render is not working but I couldn't solve it.
@TheNerdyCanuck
@TheNerdyCanuck Жыл бұрын
Yeah I think I forgot this part, I can help if you still need it.
@mawebtech
@mawebtech Жыл бұрын
awesome tutorial thank you.. I just wondering how can i use that socket instance another place such as controllers. It needs to have server instance that ServerSocket class thats why i can not use any other page
@TheNerdyCanuck
@TheNerdyCanuck Жыл бұрын
You can use it with controllers, thats the point of the static instance. Is that what you mean?
@mawebtech
@mawebtech Жыл бұрын
​@@TheNerdyCanuck actually how to get use in a that controller so another page usage because instance wants a httpServer arg. in main.js file there is a const httpServer = http.createServer(application); but in controller there isn't. please forgive my newbie. In the video, I only see its use in the main directory, but how can I use the same instance on a different controller page?
@user-qd7pz3nh8e
@user-qd7pz3nh8e Жыл бұрын
where to find the starter please?
@RdozeTV
@RdozeTV Жыл бұрын
Mind to share what keyboard you are using?
@TheNerdyCanuck
@TheNerdyCanuck Жыл бұрын
Logitech wireless mechanical it's awesome
@josemanuelulloavasquez3559
@josemanuelulloavasquez3559 Жыл бұрын
How could I split the listeners into separate files based on that structure?
@forli69
@forli69 Жыл бұрын
you just have to create a function that defines the listeners in another file. Just like he did with start listeners. Example: Inside of clientEvents.ts you create a function clientEvents with all your events for the clients and inside of the StartListeners function you call the clientEvents function.
@keepfeatherinitbrothaaaa
@keepfeatherinitbrothaaaa Жыл бұрын
I have a problem. My structure is as follows: - In my UI client component I have an onSubmitMessageHandler where i socket.emit("newMessage", 'some message val'). I'm importing socket from useContext(SocketContext).SocketState - On the server I listen for this emit and then react by calling socket.broadcast.emit("updateMessages", ). - Then in the SocketContextComponent, in the StartListeners function, I have a socket.on("updateMessages") listener which sends a SocketDispatch where I then update the messages state (an array of strings). My dispatch logic defined in the SocketContext is correct and working. Now the problem is that my dispatch always fires twice. I've been console.logging and the double call doesn't happen on the server, it happens in this last socket.on("updateMessages") listener. What am I missing? I've tried wrapping the onSubmitMessageHandler with a useCallback and an empty dependency array, wrapping the entire component in React.memo, but it's still making duplicate dispatch calls.
@TheNerdyCanuck
@TheNerdyCanuck Жыл бұрын
Remove the "react.strictmode" from the index file, see if that helps
@keepfeatherinitbrothaaaa
@keepfeatherinitbrothaaaa Жыл бұрын
@@TheNerdyCanuck Hey. Thanks for the reply. That did resolve the issue, however this seems to be more of a workaround than a permanent solution. In other words, I'm curious if there's some anti-pattern that I'm using that could be causing this issue, or if there could be some structural improvements made to avoid it altogether.
@TheNerdyCanuck
@TheNerdyCanuck Жыл бұрын
@@keepfeatherinitbrothaaaa I thought the same, but logically I don't understand how it could be calling twice. If someone explains it to me one day I'll let you know :)
@alvarmarkhester7194
@alvarmarkhester7194 Жыл бұрын
@@TheNerdyCanuck StrictMode just mounts component and remounts, so all the code is running twice. This is to make sure effects clean up if needed, cleaning up ie disconnecting from socker properly, so socketIO is perfect example why this check is nice to have.
@alvarmarkhester7194
@alvarmarkhester7194 Жыл бұрын
@@TheNerdyCanuck great video, thanks for nice structure and good pace!
Socket.io + ReactJS Tutorial | Learn Socket.io For Beginners
32:35
He bought this so I can drive too🥹😭 #tiktok #elsarca
00:22
Elsa Arca
Рет қаралды 44 МЛН
女孩妒忌小丑女? #小丑#shorts
00:34
好人小丑
Рет қаралды 99 МЛН
Matching Picture Challenge with Alfredo Larin's family! 👍
00:37
BigSchool
Рет қаралды 52 МЛН
SCHOOLBOY. Мама флексит 🫣👩🏻
00:41
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 6 МЛН
Realtime Notification App Using React and Socket.io
1:06:20
Lama Dev
Рет қаралды 170 М.
TypeScript Origins: The Documentary
1:21:36
OfferZen Origins
Рет қаралды 285 М.
WebSocket in NodeJS | Socket.IO - Real Time Messaging
32:27
Piyush Garg
Рет қаралды 164 М.
Realtime Chat With Users & Rooms - Socket.io, Node & Express
58:45
Traversy Media
Рет қаралды 877 М.
From React to React Native in 12 Minutes
12:33
Simon Grimm
Рет қаралды 14 М.
Build an AI RAG Application with LangChain & Next.js
33:34
Dave Gray
Рет қаралды 20 М.
NestJS Websockets Tutorial #1 - Creating a Websocket Gateway Server
20:37
Anson the Developer
Рет қаралды 56 М.
Why aren't you using Fastify? Or Koa? Or NestJS?
9:58
Maximilian Schwarzmüller
Рет қаралды 48 М.
He bought this so I can drive too🥹😭 #tiktok #elsarca
00:22
Elsa Arca
Рет қаралды 44 МЛН