Server-Sent Events with NestJS

Sdílet
Vložit
  • čas přidán 29. 05. 2022
  • 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.

Komentáře • 88

  • @mariusespejo
    @mariusespejo  Před 2 lety +15

    Just realized I misspelled “server” within the first 5 seconds of the video… woops 😅 🤦‍♂️

    • @dinoscheidt
      @dinoscheidt Před 2 lety +1

      Well an engineer he is 😅. Good video!

    • @josephtsegen7071
      @josephtsegen7071 Před 2 lety

      Another lovely video Marius, being follow your nest tutorials and you have really been helpful.

    • @nodeguru7663
      @nodeguru7663 Před rokem

      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 Před rokem +3

    My all time NestJS guy thank you so much for this and for all of your content

  • @GeneralA6
    @GeneralA6 Před 2 lety

    love your videos, you hooked me on nestjs. coming from angular this is a bliss on the server side

  • @iterative3605
    @iterative3605 Před 2 lety

    As always, nice job! I learnt about SSE 2 weeks ago with Mercure but looks like a promising alternative to web sockets.

  • @vadimivanovich1827
    @vadimivanovich1827 Před rokem

    Thank you for this useful video! Never heard about this technique before.

  • @ItzAnameOk
    @ItzAnameOk Před 2 lety

    always good to see another banger video

  • @CaioCesarPS
    @CaioCesarPS Před 2 lety +4

    Amazing Tutorial, please do the video about observables :)

  • @dawid_dahl
    @dawid_dahl Před rokem

    Wow, I didn’t know about this. Thank you! 🤩🙏🏻

  • @kellslte
    @kellslte Před 11 měsíci

    Thank you so much for this wonderful video!

  • @amitzulan4545
    @amitzulan4545 Před 2 lety

    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  Před 2 lety

      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

  • @user-so7po2op2e
    @user-so7po2op2e Před 2 lety

    the best NestJS lessons, everything is clear and with simple examples! can you please tell which font for VS Code you use?

    • @mariusespejo
      @mariusespejo  Před 2 lety

      Thanks! I think just the standard font? I don’t remember changing it

  • @deevally
    @deevally Před 2 lety

    Learnt Something new. Keep it up.

  • @rohit21061993
    @rohit21061993 Před rokem

    Really helpful
    Thank you Marius.

  • @sundayomolade8185
    @sundayomolade8185 Před 2 lety

    just what I was looking for

  • @devitosolucoes7534
    @devitosolucoes7534 Před rokem +2

    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  Před rokem +3

      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 Před rokem

      @@mariusespejo Thank you so much for your time and link, i'll take a look

  • @zawriter4783
    @zawriter4783 Před rokem

    More videos please, thanks!!!

  • @erlanggadewasakti
    @erlanggadewasakti Před 2 lety

    nice tutorial, thanks

  • @rism8345
    @rism8345 Před 2 lety

    Appreciate the content!

  • @subashc6493
    @subashc6493 Před 2 lety +2

    Great content on youtube when compare to others.
    It would be great if you also make videos on deployments :)

    • @mariusespejo
      @mariusespejo  Před 2 lety

      Thank you!! Deployment is a large topic but definitely thinking of ways to incorporate it in the channel

    • @subashc6493
      @subashc6493 Před 2 lety

      @@mariusespejo Thanks, it would be really helpful to many us with deployment video. Please make incorporate to this channel :)

  • @Omer23007
    @Omer23007 Před rokem

    wow!! nice video !! please make observable and rx video

  • @6365bharath
    @6365bharath Před rokem

    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  Před rokem

      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

  • @socialcatalyst2608
    @socialcatalyst2608 Před 2 lety

    Hi 👋👋👋 thanks for your content

  • @diskodenso9329
    @diskodenso9329 Před rokem

    hey Marius, really nice videos - thanks a lot! How could we close this event now using rxjs ?

    • @diskodenso9329
      @diskodenso9329 Před rokem

      ok it seems the @sse decorator is taking care of that when returning an observable...

  • @danielguldbergaaes6432
    @danielguldbergaaes6432 Před 2 lety +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  Před 2 lety +1

      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 Před 2 lety

      @@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  Před 2 lety

      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 Před 2 lety

      @@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  Před 2 lety +1

      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/

  • @dawid_dahl
    @dawid_dahl Před rokem

    (Also, I’d love a video about RxJs in a Nest context.)

  • @uzeyrozcan
    @uzeyrozcan Před rokem

    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  Před rokem

      Using the EventSource API as mentioned and shown in the video.

  • @zackOverflow
    @zackOverflow Před rokem

    Thumbs up Marius. Can I use this logic to implement in-app notifications?

    • @mariusespejo
      @mariusespejo  Před rokem +1

      Totally! As long as the 6 tab limitation isn’t a problem for you

    • @zackOverflow
      @zackOverflow Před rokem

      @@mariusespejo thank you, you are the best

  • @sandeepdravidam7989
    @sandeepdravidam7989 Před 2 lety

    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  Před 2 lety

      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 Před 2 lety

      @@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 Před 2 lety

      @@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

  • @bennisondevadoss1528
    @bennisondevadoss1528 Před rokem

    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

  • @hadirahmani7943
    @hadirahmani7943 Před rokem

    is there this limitation when we connecting from for example mobile application?

    • @mariusespejo
      @mariusespejo  Před rokem

      Depends on the mobile browser itself if it supports the client api or not

  • @trale2431
    @trale2431 Před 2 lety

    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  Před 2 lety +1

      Good topic idea, I’ll see if I can cover it at another time, thanks!

  • @dimwel
    @dimwel Před rokem

    Hi! Thank you for the great content, can you explain how i can use SSE for nuxt3 please?

    • @mariusespejo
      @mariusespejo  Před rokem

      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

  • @enrico8730
    @enrico8730 Před rokem

    Can you use react with this as client side?

  • @undocumentox
    @undocumentox Před rokem

    How can we avoid interval, I just want to run when for examplen when some user login?

    • @mariusespejo
      @mariusespejo  Před rokem +1

      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 Před rokem

      @@mariusespejo thanks brother, I will check it out and testing it 🫡

  • @adelprog
    @adelprog Před rokem

    Can i get a specific event for authenticated users ?!

    • @mariusespejo
      @mariusespejo  Před rokem

      It’s up to you what event you trigger so yes

  • @thelanelim92
    @thelanelim92 Před rokem

    Yo, Marius, u ok? Haven’t posted in a while… hope all is well.

    • @mariusespejo
      @mariusespejo  Před rokem +2

      Hi Milena! Thanks for checking in, all is good just been busy. Will be posting more soon 🙂

  • @soulofjack7294
    @soulofjack7294 Před 2 lety

    Please make a notification feature with sse

    • @mariusespejo
      @mariusespejo  Před 2 lety

      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 Před 2 lety

      @@mariusespejo I am trying but I am facing some issues with observables, i guess I need to brush up observables concept a little bit

  • @zawriter4783
    @zawriter4783 Před 2 lety

    Hey Man,
    Please keep posting my videos 😁
    Thanks a lot for your effort and time

  • @sangnguyenthe5112
    @sangnguyenthe5112 Před 2 lety

    how about saga NestJS ♥

  • @user-sl1tw9vg8x
    @user-sl1tw9vg8x Před rokem

    But how to send SSE, for example, at the time of creating a new post, and not just in an endless loop?

    • @mariusespejo
      @mariusespejo  Před rokem

      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 Před rokem

      @@mariusespejo that is, if i run the this.sendEvent(anyData) function from the neighboring endpoint, all clients will receive the message?

    • @mariusespejo
      @mariusespejo  Před rokem

      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  Před rokem +1

      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 Před rokem

      @@mariusespejo this is actually a very complex topic and the simple looping example never covers the topic

  • @NhileshABaua
    @NhileshABaua Před 2 lety

    Looks like a deepfake of SignalR.. that too had issues and this one is also has... nothing beats the sockets.

    • @mariusespejo
      @mariusespejo  Před 2 lety +1

      Well this is a standard API not an ASP.NET library like SignalR. Every solution has tradeoffs, even websockets

    • @NhileshABaua
      @NhileshABaua Před 2 lety

      @@mariusespejo Agreed, can we use SSE to handle graphql subscriptions , any thoughts on that?

    • @mariusespejo
      @mariusespejo  Před 2 lety

      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 Před 2 lety

    Not helpful.