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

docs: add example of server-side events in controllers.md #2759

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions docs/docs/controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,47 @@ This example will produce a response with status code 400 and "Not a number" mes
See our guide on [HttpExceptions to throw customer HttpExceptions](/docs/throw-http-exceptions.md)
:::

## Server-side events

::: warning
`compression` middleware should be disabled for correct SSE work
:::

### Example

```typescript
import { Controller } from '@tsed/di';
import { Res } from '@tsed/common';
import { Get } from '@tsed/schema';

@Controller('/sse')
export class Ctrl {
@Get('/events')
events(@Res() response: Res) {
let intervalId: ReturnType<typeof setInterval>;

response.on('close', () => {
console.log('connection closed');
clearInterval(intervalId);
});
response.on('end', () => {
console.log('connection ended');
clearInterval(intervalId);
});

response.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});

intervalId = setInterval(() => {
response.write(`data: Current timestamp: ${Date.now()}\n\n`);
}, 1000);
}
}
```

## Inject Request and Response

You can use a decorator to inject the Request in order to retrieve information from the request that you cannot get
Expand Down
Loading