Тёмный
No video :(

Server-Sent Events with NestJS 

Marius Espejo
Подписаться 29 тыс.
Просмотров 31 тыс.
50% 1

In this super quick tutorial we are going to look at how to allow a server to send data to a client at any time using server-sent events. We'll also take a quick look at the EventSource API which is the interface that we're use to connect the client to the server.

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

 

25 авг 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 88   
@mariusespejo
@mariusespejo 2 года назад
Just realized I misspelled “server” within the first 5 seconds of the video… woops 😅 🤦‍♂️
@dinoscheidt
@dinoscheidt 2 года назад
Well an engineer he is 😅. Good video!
@josephtsegen7071
@josephtsegen7071 2 года назад
Another lovely video Marius, being follow your nest tutorials and you have really been helpful.
@nodeguru7663
@nodeguru7663 Год назад
Hello @Marius can you explain deployment of NestJs app with TypeORM-MySql database from scratch because I'm stuck at the production. Please provide one video from scratch which basically includes NestJs MySQL and TypeORM deployment.
@metatronicx
@metatronicx 2 года назад
My all time NestJS guy thank you so much for this and for all of your content
@CaioCesarPS
@CaioCesarPS 2 года назад
Amazing Tutorial, please do the video about observables :)
@vadimivanovich1827
@vadimivanovich1827 Год назад
Thank you for this useful video! Never heard about this technique before.
@GeneralA6
@GeneralA6 2 года назад
love your videos, you hooked me on nestjs. coming from angular this is a bliss on the server side
@ItzAnameOk
@ItzAnameOk 2 года назад
always good to see another banger video
@iterative3605
@iterative3605 2 года назад
As always, nice job! I learnt about SSE 2 weeks ago with Mercure but looks like a promising alternative to web sockets.
@kellslte
@kellslte Год назад
Thank you so much for this wonderful video!
@dawid_dahl
@dawid_dahl Год назад
Wow, I didn’t know about this. Thank you! 🤩🙏🏻
@sundayomolade8185
@sundayomolade8185 2 года назад
just what I was looking for
@devitosolucoes7534
@devitosolucoes7534 Год назад
Hey, Marius, great lesson, thank you. I have a question: we are building a CRM using nestJS and basically we want to automatiically refresh the users screen whenever an opportunity is created or a funnel step is changed, deleted or created... So the backend should know every client so that it can fire the events to the right person. Do you think server-sent events would be the way to go? Or websockets? I just don't know how the server side events will differentiate each client. Is there any way the client can identify yourself? THanks
@mariusespejo
@mariusespejo Год назад
There’s definitely ways you can identify the client, like some ideas here: stackoverflow.com/questions/34992442/how-server-sent-event-send-response-to-a-specific-client If you’re authenticating users you can set a cookie that determines their session id, that should come along with the request. If you’d like to make a comparison with the setup for nest specifically, I do have a websockets video with Nest in the channel
@devitosolucoes7534
@devitosolucoes7534 Год назад
@@mariusespejo Thank you so much for your time and link, i'll take a look
@mangola-x8u
@mangola-x8u Год назад
Really helpful Thank you Marius.
@subashc6493
@subashc6493 2 года назад
Great content on youtube when compare to others. It would be great if you also make videos on deployments :)
@mariusespejo
@mariusespejo 2 года назад
Thank you!! Deployment is a large topic but definitely thinking of ways to incorporate it in the channel
@subashc6493
@subashc6493 2 года назад
@@mariusespejo Thanks, it would be really helpful to many us with deployment video. Please make incorporate to this channel :)
@deevally
@deevally 2 года назад
Learnt Something new. Keep it up.
@user-so7po2op2e
@user-so7po2op2e 2 года назад
the best NestJS lessons, everything is clear and with simple examples! can you please tell which font for VS Code you use?
@mariusespejo
@mariusespejo 2 года назад
Thanks! I think just the standard font? I don’t remember changing it
@zawriter4783
@zawriter4783 2 года назад
More videos please, thanks!!!
@rism8345
@rism8345 2 года назад
Appreciate the content!
@mariusespejo
@mariusespejo 2 года назад
Thank you! Glad you like it
@Omer23007
@Omer23007 Год назад
wow!! nice video !! please make observable and rx video
@erlanggadewasakti
@erlanggadewasakti 2 года назад
nice tutorial, thanks
@danielguldbergaaes6432
@danielguldbergaaes6432 2 года назад
Please do make a video about RXJS and Observables! Can I ask why EventMessage has the type of "string | Object"? I looked at the docs and it states that the return value can only be a string or an event stream? Is the type of the object just to be sure to cover the event-stream?
@mariusespejo
@mariusespejo 2 года назад
Yeah pretty much to match the spec, if you read about it under the hood what’s really being sent by NestJS is something that looks like this: developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#examples Which you’ll notice pretty much it makes you assume the payload of the event is generally either a basic string or an object.
@danielguldbergaaes6432
@danielguldbergaaes6432 2 года назад
@@mariusespejo Thanks mate. So far I have been following along with your tutorials and I really love NestJS, for the most :) But I have to create an API with is going to handle a large amount of data, sometimes I will have to recieve a lot of data, transform it and inserting between 1K-10K rows in a database. Do you think NestJS with TypeORM could handle that? Or woulda .NET Core API with React on top of it be a better solution for this?
@mariusespejo
@mariusespejo 2 года назад
nestjs and typeorm are just frameworks/libraries. On their own they don’t determine what can or can’t be handled based on load, that sounds like a question more for your infrastructure… e.g. are you thinking about scalability… does your infrastructure allow spinning up additional instances of the server when one can’t handle all the requests… does your database handle multiple concurrent writes, etc. etc. In general what is “better” can be very subjective. There’s a lot of variables at play including ecosystems… if you can’t decide I suggest doing a proof of concept with your top options and go from there
@danielguldbergaaes6432
@danielguldbergaaes6432 2 года назад
@@mariusespejo Thanks once again for the thorouw reply Marius! I guess what I was thinking about the most is that I would be afraid that Node/Nestjs would be blocking incommin requests to while the application was inserting ~10K rows in the database and at some point it would fail. The whole infrastructure and scalability taken care of and it should be fairly easy to scale horizontally, however I would love if I didn't have to do so in order to keep the cost low. But I will probably end up trying to make two small demos with a lot of inserts to test the performance. Thanks
@mariusespejo
@mariusespejo 2 года назад
I highly recommend reading about the event loop and how it works. I think a lot of people assume “oh it’s single thread so long tasks are blocking” it’s not really that simple. E.g. from my understanding database IO is an asynchronous event and should be non-blocking. If you’re having to transform that data synchronously before or after inserting to the database, that probably is blocking however how much that becomes a problem depends on your underlying algorithm. Also you can setup your API to not necessarily wait for the entire thing to be done before responding… you could take an event-driven approach. In general I think that NodeJS scales fairly well if used properly, even on a single server. nodejs.org/en/docs/guides/blocking-vs-non-blocking/
@socialcatalyst2608
@socialcatalyst2608 2 года назад
Hi 👋👋👋 thanks for your content
@mariusespejo
@mariusespejo 2 года назад
👋👋👋
@hadirahmani7943
@hadirahmani7943 Год назад
is there this limitation when we connecting from for example mobile application?
@mariusespejo
@mariusespejo Год назад
Depends on the mobile browser itself if it supports the client api or not
@amitzulan4545
@amitzulan4545 2 года назад
thanks for the video! a quick question, if I use the sse in the same controller with a rest API would both work seperately?
@mariusespejo
@mariusespejo 2 года назад
I think so, I don’t see any problems with that. You’re really just defining separate routes, and if one happens to be for SSE I think it should be fine
@dawid_dahl
@dawid_dahl Год назад
(Also, I’d love a video about RxJs in a Nest context.)
@bennisondevadoss1528
@bennisondevadoss1528 Год назад
For me it does not works, What the error is, I got the error like evetsource.onmessage is not a function, If anyone know that answer please answer
@6365bharath
@6365bharath Год назад
How would you send a message from a queue like SQS to the client through SSE? The code you are showing is very basic and is already available on the nestjs offlcial doc.
@mariusespejo
@mariusespejo Год назад
Yes… it’s a beginner tutorial. You can take a look at this package as one way to consume sqs www.npmjs.com/package/@ssut/nestjs-sqs
@uzeyrozcan
@uzeyrozcan Год назад
So if I wanted to return this with modern javascript libraries, how could I do it? How can I listen to this with Angular or React?
@mariusespejo
@mariusespejo Год назад
Using the EventSource API as mentioned and shown in the video.
@enrico8730
@enrico8730 Год назад
Can you use react with this as client side?
@mariusespejo
@mariusespejo Год назад
Yes, should work in any framework
@adelprog
@adelprog Год назад
Can i get a specific event for authenticated users ?!
@mariusespejo
@mariusespejo Год назад
It’s up to you what event you trigger so yes
@sandeepdravidam7989
@sandeepdravidam7989 2 года назад
i have scenario regarding caching a request,if 200 concurrent users hitting an api ,how should I queue 199 requests,n execute single request api and cache it,once caching is done all the 199 requests need to be processed n data need to be fetched from cached value
@mariusespejo
@mariusespejo 2 года назад
I would imagine it’s not really about queueing at all, … I imagine you have some kind of store for the cache? Then your store just needs to be updated to cache the response of the first request… then any requests made after that simply needs to first check if the value is in the store and if not then it must be the first request. If you really want to try some kind of waiting/queue behavior I imagine you can use locks to pull that off. E.g. if data not in cache, acquire lock and fetch data, store data in cache. If data not in cache and unable to acquire lock, just wait (it means another request already initiated)
@sandeepdravidam7989
@sandeepdravidam7989 2 года назад
@@mariusespejo the main purpose is caching n storing ,but when concurrent requests are made ,node is async so all will try to contact db,though I write any check to get the value from cache. How could I avoid load at db. First request to that api need to contact db and other requests need to be queued until I get response from first request then i will cache the value ,later i will pass requests in the queue which can get the values from cache as we are writing a check logic in the beginning
@sandeepdravidam7989
@sandeepdravidam7989 2 года назад
@@mariusespejo i know my question is not related to the video,but i thought u have that exposure to resolve the issues,as your nestjs videos are so informative n precise n smooth to follow up
@user-sl1tw9vg8x
@user-sl1tw9vg8x Год назад
But how to send SSE, for example, at the time of creating a new post, and not just in an endless loop?
@mariusespejo
@mariusespejo Год назад
To be clear the loop is just an example. You need to basically subscribe or listen to some kind of event, which then triggers your SSE. You can accomplish this with a couple different ways using observables or event emitters
@user-sl1tw9vg8x
@user-sl1tw9vg8x Год назад
@@mariusespejo that is, if i run the this.sendEvent(anyData) function from the neighboring endpoint, all clients will receive the message?
@mariusespejo
@mariusespejo Год назад
The point is not to directly call this.sendEvent, but that sendEvent itself should be subscribed to something, and it will automatically broadcast to clients when what it is subscribed to gets a new event
@mariusespejo
@mariusespejo Год назад
Couple examples here using an observable stream that you can add events to, or using event emitter stackoverflow.com/questions/67202527/can-we-use-server-sent-events-in-nestjs-without-using-interval
@user-sl1tw9vg8x
@user-sl1tw9vg8x Год назад
@@mariusespejo this is actually a very complex topic and the simple looping example never covers the topic
@zackOverflow
@zackOverflow Год назад
Thumbs up Marius. Can I use this logic to implement in-app notifications?
@mariusespejo
@mariusespejo Год назад
Totally! As long as the 6 tab limitation isn’t a problem for you
@zackOverflow
@zackOverflow Год назад
@@mariusespejo thank you, you are the best
@undocumentox
@undocumentox Год назад
How can we avoid interval, I just want to run when for examplen when some user login?
@mariusespejo
@mariusespejo Год назад
You’ll need to add some kind of event emitter and make your sse method be subscribed to those login events. You might get some ideas here: docs.nestjs.com/techniques/events
@undocumentox
@undocumentox Год назад
@@mariusespejo thanks brother, I will check it out and testing it 🫡
@diskodenso9329
@diskodenso9329 Год назад
hey Marius, really nice videos - thanks a lot! How could we close this event now using rxjs ?
@diskodenso9329
@diskodenso9329 Год назад
ok it seems the @sse decorator is taking care of that when returning an observable...
@thelanelim92
@thelanelim92 Год назад
Yo, Marius, u ok? Haven’t posted in a while… hope all is well.
@mariusespejo
@mariusespejo Год назад
Hi Milena! Thanks for checking in, all is good just been busy. Will be posting more soon 🙂
@dimwel
@dimwel Год назад
Hi! Thank you for the great content, can you explain how i can use SSE for nuxt3 please?
@mariusespejo
@mariusespejo Год назад
The consumings side should be the same, it’s the same api for client. The server/publishing side I’m not sure for nuxt specifically, I suggest looking at docs
@soulofjack7294
@soulofjack7294 2 года назад
Please make a notification feature with sse
@mariusespejo
@mariusespejo 2 года назад
I mean pretty much if you take what I did here, and added something on the client side which pops up a notification… that’s pretty much it
@soulofjack7294
@soulofjack7294 2 года назад
@@mariusespejo I am trying but I am facing some issues with observables, i guess I need to brush up observables concept a little bit
@trale2431
@trale2431 2 года назад
Thank you very much for making such a good video. Can you making a video about GraphQL pagination for NestJS? I search on google for a whole week and cant find a tutorial (sorry im new to node and nestjs). Anyway thanks for greate video.
@mariusespejo
@mariusespejo 2 года назад
Good topic idea, I’ll see if I can cover it at another time, thanks!
@sangnguyenthe5112
@sangnguyenthe5112 2 года назад
how about saga NestJS ♥
@zawriter4783
@zawriter4783 2 года назад
Hey Man, Please keep posting my videos 😁 Thanks a lot for your effort and time
@NhileshABaua
@NhileshABaua 2 года назад
Looks like a deepfake of SignalR.. that too had issues and this one is also has... nothing beats the sockets.
@mariusespejo
@mariusespejo 2 года назад
Well this is a standard API not an ASP.NET library like SignalR. Every solution has tradeoffs, even websockets
@NhileshABaua
@NhileshABaua 2 года назад
@@mariusespejo Agreed, can we use SSE to handle graphql subscriptions , any thoughts on that?
@mariusespejo
@mariusespejo 2 года назад
Traditionally most graphql implementations, e.g. apollo, have used websockets to implement subscriptions. Wundergraph wrote about using SSE instead but I don’t know much about it yet wundergraph.com/blog/deprecate_graphql_subscriptions_over_websockets
@uwemnkereuwem6272
@uwemnkereuwem6272 2 года назад
Not helpful.
@mariusespejo
@mariusespejo 2 года назад
😂
Далее
Server-Sent Events Crash Course
29:48
Просмотров 90 тыс.
Nest.js | Events & Task Scheduling Tutorial
15:04
Просмотров 33 тыс.
Avaz Oxun - 10 yillik yubiley konsert dasturi 2023
2:52:33
Decouple your NestJS code with this technique!
11:37
Просмотров 36 тыс.
When RESTful architecture isn't enough...
21:02
Просмотров 272 тыс.
WTF Do These Even Mean
13:44
Просмотров 83 тыс.
Node.js is a serious thing now… (2023)
8:18
Просмотров 642 тыс.
Don't Use Websockets (Until You Try This…)
6:46
Просмотров 297 тыс.
Can we combine a NestJS app with React?
20:43
Просмотров 31 тыс.
System Design has never been easier
14:26
Просмотров 115 тыс.
Why aren't you using Fastify? Or Koa? Or NestJS?
9:58
Why NestJS?
3:47
Просмотров 19 тыс.