You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: sse/README.md
+35-4Lines changed: 35 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,22 +45,53 @@ Server-Sent Events (SSE) allow servers to push updates to the client over a sing
45
45
46
46
- **GET /**: Index page
47
47
- **GET /sse**: SSE route
48
+
- **PUT /publish**: Send messages via SSE
48
49
49
50
## Example Usage
50
51
52
+
By default, the example will run on port `3000`; this can be changed by modifying the `appPort` constant in`main.go`
53
+
51
54
1. Open your browser and navigate to `http://localhost:3000`.
52
55
2. The client will automatically connect to the SSE endpoint and start receiving updates from the server.
56
+
3. The `/sse` endpoint will publish the current time to the client every two seconds
57
+
58
+
### Custom Messages
59
+
60
+
To send a custom message, send a `PUT` request to the `/publish` endpoint in the following JSON format
61
+
62
+
```json
63
+
{
64
+
"message": "Hello, World!"
65
+
}
66
+
```
67
+
68
+
Messages sent to the `/publish` endpoint will be added to a queue that is read from in FIFO order. You can test this
69
+
by using curl in an iterator
70
+
71
+
If you are using the Bash or Zsh shell:
72
+
```sh
73
+
foriin {1..10};do
74
+
curl -X PUT -H 'Content-type: application/json' --data "{\"message\":\"SSE TEST $i\"}" http://localhost:3000/publish
75
+
done
76
+
```
77
+
78
+
If you are using fish:
79
+
```sh
80
+
foriin (seq 1 10)
81
+
curl -X PUT -H 'Content-type: application/json' --data "{\"message\":\"SSE TEST $i\"}" http://localhost:3000/publish
82
+
end
83
+
```
84
+
85
+
Once published, your added messages will begin appearing in the output at `http://localhost:3000`. Once the queue is empty
86
+
and no user-published messages are left, `/sse` will return to it's standard behavior of displaying the current time.
87
+
53
88
54
89
## Code Overview
55
90
56
91
### `main.go`
57
92
58
93
The main Go file sets up the Fiber application and handles the SSE connections. It includes the necessary configuration to send events to the client.
59
94
60
-
### `index.html`
61
-
62
-
The HTML file provides a simple user interface to connect to the SSE endpoint and display the received messages.
63
-
64
95
## Additional Information
65
96
66
97
Server-Sent Events (SSE) is a standard allowing servers to push data to web clients over HTTP. Unlike WebSockets, which require a full-duplex connection, SSE uses a unidirectional connection from the server to the client. This makes SSE simpler to implement and more efficient for scenarios where only the server needs to send updates.
0 commit comments