Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sse): Implement Server-Sent Events #4842

Merged
merged 3 commits into from
Oct 30, 2020

Conversation

soyuka
Copy link
Contributor

@soyuka soyuka commented May 25, 2020

PR Checklist

PR Type

[x] Feature see #4826

See discussion in #4826. You can test this using the new sample 28.

Usage:

@Controller()
export class AppController {
  @Get()
  getHello(): string {
    return `
    <script type="text/javascript">
      const ee = new EventSource('/sse')
      ee.onmessage = ({data}) => {
        console.log('New message', JSON.parse(data))
      }
    </script>
    `;
  }

  @Sse('/sse')
  sse(): Observable<MessageEvent> {
    return interval(1000).pipe(map((_) => ({ data: { hello: 'world' } })));
  }
}

@@ -0,0 +1,75 @@
<p align="center">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the 28-sse? I don't think it makes sense to create a separate sample for this (likewise, we don't have dedicated samples for @Redirect() or any similar decorators)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this @sse directive as we could see the Websocket one. I think that this may serve as a functional test or a test application to understand how it works and to understand that it can be used for real-time updates.

@coveralls
Copy link

coveralls commented May 29, 2020

Pull Request Test Coverage Report for Build 1eed7a91-71e0-4b54-963b-382fb2a112fe

  • 62 of 63 (98.41%) changed or added relevant lines in 7 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.04%) to 94.859%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/core/services/sse-stream.service.ts 28 29 96.55%
Totals Coverage Status
Change from base Build e71e44e4-8e59-4613-8899-b4e4a3d5c916: 0.04%
Covered Lines: 4982
Relevant Lines: 5252

💛 - Coveralls

@soyuka soyuka marked this pull request as ready for review May 29, 2020 20:48
@soyuka soyuka force-pushed the feat/eventsource branch 2 times, most recently from 2d50017 to 9fb6ecb Compare May 30, 2020 00:19
@kamilmysliwiec
Copy link
Member

The overall API looks good! Would you like to create a PR to the docs? We can add it under the "Techniques" category (as a new, dedicated page) or just add a new section in the "Controllers" chapter

soyuka added a commit to soyuka/docs.nestjs.com that referenced this pull request Jul 29, 2020
@soyuka
Copy link
Contributor Author

soyuka commented Jul 29, 2020

see nestjs/docs.nestjs.com#1387 lmk if I should rebase

@soyuka
Copy link
Contributor Author

soyuka commented Jul 30, 2020

rebased and added docs

@jdgriffith
Copy link

Any chance this will land soon? Our team could use this feature :)

@soyuka
Copy link
Contributor Author

soyuka commented Aug 18, 2020

Any chance this will land soon? Our team could use this feature :)

Would you be able to test my branch and make some feedbacks?

@soyuka soyuka force-pushed the feat/eventsource branch 2 times, most recently from 7131ce2 to 3909f3a Compare August 19, 2020 07:04
packages/core/services/sse-stream.service.ts Outdated Show resolved Hide resolved
packages/core/services/sse-stream.service.ts Outdated Show resolved Hide resolved
packages/core/services/sse-stream.service.ts Outdated Show resolved Hide resolved
packages/core/services/sse-stream.service.ts Outdated Show resolved Hide resolved
packages/core/services/sse-stream.service.ts Outdated Show resolved Hide resolved
packages/core/services/sse-stream.service.ts Outdated Show resolved Hide resolved
@soyuka soyuka force-pushed the feat/eventsource branch 2 times, most recently from a3eabe0 to 04c689d Compare August 19, 2020 14:18
@soyuka soyuka force-pushed the feat/eventsource branch 2 times, most recently from 5ff4fbe to 082a569 Compare August 25, 2020 13:49
@mfcodeworks
Copy link

Any chance for this being merged soon? Would love to provide SSE updates to clients

@lukeheath
Copy link

Same here! This will be a great feature add for the framework.

@soyuka
Copy link
Contributor Author

soyuka commented Sep 4, 2020

Any chance for this being merged soon? Would love to provide SSE updates to clients
Same here! This will be a great feature add for the framework.

Same comment as above: Would you be able to test my branch and make some feedbacks?

@soyuka
Copy link
Contributor Author

soyuka commented Sep 25, 2020

I have benchmarks where this hits 80k req/s pushed on a single node. I'd love to know what maintainers think of this work!

@kamilmysliwiec kamilmysliwiec added this to the 7.5.0 milestone Oct 5, 2020
@kamilmysliwiec
Copy link
Member

Thank you for this amazing contribution! This feature will be available in 7.5.0

@kamilmysliwiec kamilmysliwiec merged commit 511625c into nestjs:master Oct 30, 2020
@ruslanguns
Copy link

Nice feature! Thank you !

jmcdo29 pushed a commit to jmcdo29/docs.nestjs.com that referenced this pull request Nov 24, 2020
@ningacoding
Copy link

Hello, how do you send data from a service? to an already subscribed client.

i tried but observables only works in the method scope and when trying to emit data from another endpoint (for example, when another user/client is updating data) the observable just do not emit to the other client to receive the update.

@kamilmysliwiec
Copy link
Member

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

@nestjs nestjs locked as spam and limited conversation to collaborators Nov 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.