S42Core SSE Class
- SSE - s42-core
SSE - s42-core
The SSE
(Server-Sent Events) class from the s42-core
package simplifies the creation of real-time communication streams in web applications. It provides an easy way to send events from the server to connected clients using the Server-Sent Events protocol.
Purpose
SSE
is designed for real-time, one-way communication from the server to the client. It is particularly useful for scenarios like:
- Live notifications (e.g., new messages, system updates)
- Real-time monitoring (e.g., stock prices, server health)
- Event broadcasting (e.g., user activity in collaborative applications)
Installation
Install the s42-core
package:
Features
- Real-Time Communication: Send server events to clients with minimal overhead.
- Event Broadcasting: Broadcast events to multiple clients simultaneously.
- Ease of Use: Straightforward API to manage connections and send events.
- Integration with Controllers: Works seamlessly with
Controller
andRouteControllers
classes.
Usage
Setting Up SSE in a Controller
Sending Events
To broadcast events to all connected clients:
Example Scenario
Use Case: Real-Time Notifications
- Client Connection: Clients connect to the
/sse
endpoint to establish an SSE connection. - Broadcasting Events: The server broadcasts events (e.g., new notifications) to all connected clients.
Client-Side Example (JavaScript)
Methods
Constructor
req
: TheRequest
object from the incoming HTTP request.
getResponse(): Response
Returns the Response
object to send back to the client.
getUUID(): string
Returns a unique identifier for the SSE instance.
send(data: TypeSSEventToSend): void
Sends an event to the connected client.
data
: An object containing:eventName
: The name of the event.eventPayload
: The payload of the event.
close(): void
Closes the SSE connection.
Integration with s42-core
The SSE
class works seamlessly with the Controller
, RouteControllers
, and Server
classes from the s42-core
package. It is designed to fit into your application’s architecture effortlessly.
Advantages
- Lightweight: Minimal setup for real-time communication.
- Scalable: Efficiently handles multiple connections.
- Simple API: Intuitive methods to manage SSE connections.
- Event Flexibility: Supports custom events and data payloads.
License
This project is licensed under the MIT License. See the LICENSE file for more details.