This is a simple Nest.js application demonstrating the use of Server-Sent Events (SSE) to send real-time notifications to clients. The project includes a basic backend service to handle notifications and a frontend example to display them.
- Backend: A Nest.js server that provides endpoints to post notifications and stream notifications to clients in real-time using SSE.
- Frontend: A simple HTML page to demonstrate receiving notifications in real-time and displaying them with a notification badge.
-
Clone the repository:
git clone git@github.com:peterkracik/nestjs-server-sent-events.git cd nestjs-server-sent-events -
Install the dependencies:
yarn install
-
Start the Nest.js server:
yarn start:dev
The server will run on
http://localhost:3000. Provide a query parameteruserto the frontend to specify the user ID. For example,http://localhost:3000/?user=1.
- Description: Receives a new notification and adds it to the store.
- Request Body:
{
"message": "string",
"userId": "string"
}- Description: Returns all notifications for a given user.
- Parameters:
id: User ID
- Description: Returns a stream of notifications for a given user.
- Parameters:
id: User ID
The frontend is a simple HTML page using Tailwind CSS for styling. It demonstrates how to:
- Send notifications via a form.
- Display notifications count using a badge.
- Receive real-time notifications using SSE.
- Open the
http://localhost:3000/?user=1file in a browser. - Use the form to send notifications to a user.
- The notification count badge will update in real-time as new notifications are received.
@nestjs/common: Core Nest.js components.@nestjs/event-emitter: Used to emit and listen to events within the application.rxjs: Library for reactive programming using Observables.
This project is licensed under the MIT License.