Тёмный

WebSocket Client in TypeScript 

Nono Martínez Alonso
Подписаться 3,5 тыс.
Просмотров 9 тыс.
50% 1

How to build a client for real-time communication with WebSockets in TypeScript using the ws NPM package.
💬 Join the conversation on Discord / discord
🧠 Machine Intelligence Playlist: • 🧠 Machine Intelligence
🔴 Live Playlist: • 🧠 Live Streams
🕸 Web Development Playlist: • 🚀 Web
🍃 Getting Simple: gettingsimple.com
🎙 Podcast: gettingsimple.com/podcast
🗣 Ask Questions: gettingsimple.com/ask
💬 Discord: / discord
👨🏻‍🎨 Sketches: sketch.nono.ma
✍🏻 Blog: nono.ma
🐦 Twitter: / nonoesp
📸 Instagram: / nonoesp
==================
🎥 VIDEO CHAPTERS
==================
00:00 Introduction
00:24 Project setup
00:43 Dependencies
01:18 Configuring TypeScript and ts-node
01:51 Project files
02:21 Client Code
03:25 WebSocket event handlers
04:18 Connecting to a server
05:53 Outro

Наука

Опубликовано:

 

10 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 10   
@Steve-Richter
@Steve-Richter Год назад
I thought the purpose of web sockets was to support the socket client running in the browser. When I run this client code in the browser I get a message saying ws does not work in the browser.
Год назад
Hi, Steve! That's right, the ws library works on Node.js for server and client code but won't work in the browser. There's native WebSocket support in browsers today. Take a look at these Mozilla docs on How to write WebSocket client apps › developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications Here's a sample code to connect to a server from a browser client, log received messages to the console, and send a message to the server when we connect. ``` // Create a WebSocket client in the browser. const ws = new WebSocket("ws://localhost:1234"); // Log incoming messages to the console. ws.onmessage = function (event) { // This runs when receiving message. console.log(event.data); }; ws.onopen = () => { // This runs when we connect. // Submit a message to the server ws.send(`Hello, WebSocket! Sent from a browser client.`); }; ``` Permalink › nono.ma/e/Q2D I hope this helps! Kindly, Nono
@AustinAtchley
@AustinAtchley 9 месяцев назад
Very clear. Thanks for the video
8 месяцев назад
Thanks so much for you comment, Austin. I'm glad you found this video useful. Nono
@damcism
@damcism Год назад
Thanks for a great video. In which moment does it actually connect? This is a bit weird because you don't run anything like ws.connect() and the open event is triggered. That suggests that it opens the connection when you initialize new object, but at that moment you haven't defined "on open" event, so that shouldn't work then...
Год назад
Hi, damcism! Thanks for your comment. Did you check the server code video? ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-h5B0iOz8vVg.html In client.ts, the following line is all you need to create a client and connect to the server. const ws = new WebSocket(`ws://localhost:${port}`); Even when you define the "open" and "message" handlers afterwards, they work with the same connection, probably because the definition of those handlers happens really fast, before the connection with the server has been established. I hope this helps! Kindly, Nono
@chirkov
@chirkov Год назад
Where is TypeScript?
Год назад
Hi, Chikov! The moment you install the typescript @types/node and @types/ws packages for development (with the command below) you get TypeScript type completion for free. npm install --save-dev typescript @types/node @types/ws I hope that helps! Kindly, Nono
Далее
Flex Grids with HTML5 & CSS Flexbox
17:51
Просмотров 307
WebSocket Server in TypeScript
7:33
Просмотров 8 тыс.
УНИТАЗ В ЛЕСУ?? #shorts
00:24
Просмотров 803 тыс.
Realtime Features for React: Easier Than You Think
14:58
Don't Use Websockets (Until You Try This…)
6:46
Просмотров 288 тыс.
tRPC, gRPC, GraphQL or REST: when to use what?
10:46
Просмотров 72 тыс.