Тёмный
No video :(

Server Sent Events - An alternative to AJAX and WebSockets 

Coders Page
Подписаться 2,7 тыс.
Просмотров 12 тыс.
50% 1

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

 

25 авг 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 41   
@leonf.7893
@leonf.7893 4 года назад
Really interesting. I had never heard of SSE
@CodersPage
@CodersPage 4 года назад
That is why I made the video
@amiralikulov9300
@amiralikulov9300 4 года назад
Good explanation. It deserves more views.
@CodersPage
@CodersPage 4 года назад
Thanks a lot!
@kozie928
@kozie928 3 года назад
Great overview, thanks :)
@bluebean5929
@bluebean5929 3 года назад
Subscribed. Great job man
@williamkopanchuk
@williamkopanchuk Год назад
Thanks! 👍
@doninurramdan8634
@doninurramdan8634 Год назад
great❤
@Lorant1984
@Lorant1984 Год назад
Hey, where is your "BUY ME WHISKEY" link?
@CodersPage
@CodersPage Год назад
Never thought about it. I will create that. Since I make no money
@evstafyevandrew2198
@evstafyevandrew2198 4 года назад
Нужно переменную globalVersion указать как 1, а localVersion = 0. Тогда при первоначальной инициализации будет отрисовка данных подписчиков. Если открыть новую вкладку при уже изменённых данных они будут перерисованы.
@CodersPage
@CodersPage 3 года назад
Хорошее наблюдение
@trochymiak
@trochymiak 4 года назад
Very interesting content, thank you for this example.
@CodersPage
@CodersPage 4 года назад
My pleasure!
@applevapple3776
@applevapple3776 2 года назад
u said server connection will remain open forever is their any way to close it after user exits tab or component from the client side?
@CodersPage
@CodersPage 2 года назад
Yes, use the close function on the sse. If the varaible pointing to the serverEvent is called "sse" then it will be sse.close()
@diejuse
@diejuse 4 года назад
The problem is most hosts do not allow them. If you try to implement one in some way a lot of times they will kick you off because they are resource intensive. getting a host that will support them is expensive.
@CodersPage
@CodersPage 4 года назад
Apache hosts will have issues. I think Nodejs hosts will be fine for this architecture
@diejuse
@diejuse 4 года назад
@@CodersPage Can you tell me good Nodejs hosts?
@CodersPage
@CodersPage 4 года назад
@@diejuse choose onw that is easy to configure. For example AWS is to complex and time consuming. Probably go for Heroku or DigitalOcean... Or Azure
@NhonHa
@NhonHa 4 года назад
This way is better than websockets. So if host accepts websockets, then sse should also be accepted because sse only update when new version, whereas websockets is always open and sending
@kazeemodutola9617
@kazeemodutola9617 3 года назад
This is awesome, please can you share the github link?
@CodersPage
@CodersPage 3 года назад
The code is on GitHub: github.com/coderspage/nodejs-server-sent-events.git
@JamesSmith-cm7sg
@JamesSmith-cm7sg 3 года назад
Thank you - but what is the benefit compared with web sockets, because you still have an open connection, but you've limited it.
@CodersPage
@CodersPage 3 года назад
Consumes less battery in mobile devices. Does not need to open ports in firewalls since runs natively on port 80. Re-connects automatically.
@evstafyevandrew2198
@evstafyevandrew2198 4 года назад
Спасибо огромное! Ты крутой!
@CodersPage
@CodersPage 4 года назад
Мне приятно помочь вам
@jimitshah7636
@jimitshah7636 3 года назад
Hey, nice explaination... I had a question. What if I want to send/broadcast events/messages to say 10s of millions of clients/subscribers. How can we scale server sent events to achieve that?
@CodersPage
@CodersPage 3 года назад
Each connected client has its own SSE, therefore you could save a message in the database and each client can read that message.
@mydhe6
@mydhe6 2 года назад
@ 42:59 where you introduce and compare localVersion with GlobalVersion, how can I have the same logic if am using SSE like below; const sendEventDashboard = async (req, res) => { try { const orders = await Order.find({ agent_id: req.params.id }) .populate("agent_id") .sort({ _id: -1 }); res.writeHead(200, { "Cache-Control": "no-cache", "Content-Type": "text/event-stream", Connection: "keep-alive", }); const sseId = new Date().toDateString(); const intervalId = setInterval(() => { writeEvent(res, sseId, JSON.stringify(orders)); }, SEND_INTERVAL); res.on("close", () => { clearInterval(intervalId); res.end(); // console.log("Client closed connection browser"); }); } catch (error) { console.log(error); } }; export const getOrdersStreamDashboard = async (req, res) => { if (req.headers.accept === "text/event-stream") { sendEventDashboard(req, res); } else { res.json({ message: "Okay" }); } };
@theartist8835
@theartist8835 3 года назад
you could have used res.sendFile(absolutePath) instead of using the file module.
@CodersPage
@CodersPage 3 года назад
Yes, thank you
@theartist8835
@theartist8835 3 года назад
Thank you as well😊
@Manudoido100
@Manudoido100 4 года назад
Thank you, I really appreciate your explanation. Where can I find the source code?
@CodersPage
@CodersPage 4 года назад
I am working on my own page to upload the code. Give me until the weekend to post a link to the zip.
@Manudoido100
@Manudoido100 4 года назад
@@CodersPage Ok, thank you!
@zhejietchang7001
@zhejietchang7001 3 года назад
how to send a message to a few targeted users only?
@CodersPage
@CodersPage 3 года назад
There are no groups or rooms, so create a list with ids of the users you want to receive a message. Loop through the list in the SSEand if there is a match, send the data
@AmitAnand03
@AmitAnand03 3 года назад
you're lying... you had 3 chrome browsers open 😂😂 Great video though 👍
Далее
Server-Sent Events Crash Course
29:48
Просмотров 90 тыс.
O'ZBEK VA TOJIKLAR ROSSIYADA TERAKT UYUSHTIRISHDI!
08:41
WebRTC Crash Course
1:10:06
Просмотров 226 тыс.
React and NodeJS App | SSE and EventSource
21:02
Просмотров 5 тыс.
Advanced Node.js Crash Course | New Year Gift!
2:45:01
Просмотров 134 тыс.
Node.js is a serious thing now… (2023)
8:18
Просмотров 642 тыс.