Server-Sent Events with NestJS

Поділитися
Вставка
  • Опубліковано 23 січ 2025

КОМЕНТАРІ • 88

  • @mariusespejo
    @mariusespejo  2 роки тому +16

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

    • @dinoscheidt
      @dinoscheidt 2 роки тому +1

      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 2 роки тому

      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 роки тому +3

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

  • @GeneralA6
    @GeneralA6 2 роки тому

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

  • @CaioCesarPS
    @CaioCesarPS 2 роки тому +4

    Amazing Tutorial, please do the video about observables :)

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

  • @vadimivanovich1827
    @vadimivanovich1827 Рік тому

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

  • @ItzAnameOk
    @ItzAnameOk 2 роки тому

    always good to see another banger video

  • @devitosolucoes7534
    @devitosolucoes7534 Рік тому +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  Рік тому +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 Рік тому

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

  • @danielguldbergaaes6432
    @danielguldbergaaes6432 2 роки тому +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 роки тому +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 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 роки тому +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/

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

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

  • @6365bharath
    @6365bharath 2 роки тому

    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  2 роки тому

      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

  • @undocumentox
    @undocumentox 2 роки тому

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

    • @mariusespejo
      @mariusespejo  2 роки тому +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 2 роки тому

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

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

  • @ДанилДанилов-н7м
    @ДанилДанилов-н7м 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

  • @kellslte
    @kellslte Рік тому

    Thank you so much for this wonderful video!

  • @enrico8730
    @enrico8730 Рік тому

    Can you use react with this as client side?

  • @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  Рік тому +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 Рік тому

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

  • @dawid_dahl
    @dawid_dahl 2 роки тому

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

  • @adelprog
    @adelprog Рік тому

    Can i get a specific event for authenticated users ?!

    • @mariusespejo
      @mariusespejo  Рік тому

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

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

  • @subashc6493
    @subashc6493 2 роки тому +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 :)

  • @sundayomolade8185
    @sundayomolade8185 2 роки тому

    just what I was looking for

  • @deevally
    @deevally 2 роки тому

    Learnt Something new. Keep it up.

  • @zackOverflow
    @zackOverflow Рік тому

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

    • @mariusespejo
      @mariusespejo  Рік тому +1

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

    • @zackOverflow
      @zackOverflow Рік тому

      @@mariusespejo thank you, you are the best

  • @mangola-x8u
    @mangola-x8u Рік тому

    Really helpful
    Thank you Marius.

  • @diskodenso9329
    @diskodenso9329 2 роки тому

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

    • @diskodenso9329
      @diskodenso9329 2 роки тому

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

  • @socialcatalyst2608
    @socialcatalyst2608 2 роки тому

    Hi 👋👋👋 thanks for your content

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

  • @rism8345
    @rism8345 2 роки тому

    Appreciate the content!

  • @zawriter4783
    @zawriter4783 2 роки тому

    More videos please, thanks!!!

  • @dawid_dahl
    @dawid_dahl 2 роки тому

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

  • @Omer23007
    @Omer23007 2 роки тому

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

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

  • @thelanelim92
    @thelanelim92 2 роки тому

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

    • @mariusespejo
      @mariusespejo  2 роки тому +2

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

  • @erlanggadewasakti
    @erlanggadewasakti 2 роки тому

    nice tutorial, thanks

  • @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 роки тому +1

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

  • @sangnguyenthe5112
    @sangnguyenthe5112 2 роки тому

    how about saga NestJS ♥

  • @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 роки тому +1

      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

  • @zawriter4783
    @zawriter4783 2 роки тому

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

  • @uwemnkereuwem6272
    @uwemnkereuwem6272 2 роки тому

    Not helpful.

  • @uzeyrozcan
    @uzeyrozcan 2 роки тому

    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  2 роки тому

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