@fastify/sse 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/examples/basic.js CHANGED
@@ -1,131 +1,152 @@
1
1
  'use strict'
2
2
 
3
- const fastify = require('fastify')({ logger: true })
4
-
5
- // Register the SSE plugin
6
- fastify.register(require('../index.js'))
7
-
8
- // Basic SSE endpoint
9
- fastify.get('/events', { sse: true }, async (request, reply) => {
10
- // Send a simple message
11
- await reply.sse.send({ data: 'Hello SSE!' })
12
-
13
- // Send multiple events
14
- for (let i = 0; i < 5; i++) {
15
- await reply.sse.send({
16
- id: String(i),
17
- event: 'counter',
18
- data: { count: i, timestamp: Date.now() }
19
- })
20
- }
3
+ /**
4
+ * Example Basic with @fastify/sse
5
+ *
6
+ * Test endpoints with curl:
7
+ *
8
+ * curl -N -H "Accept: text/event-stream" http://localhost:3000/events
9
+ * curl -N -H "Accept: text/event-stream" http://localhost:3000/stream
10
+ * curl -N -H "Accept: text/event-stream" http://localhost:3000/live
11
+ * curl -N -H "Accept: text/event-stream" http://localhost:3000/headers
12
+ * curl -N -H "Accept: text/event-stream" http://localhost:3000/replay
13
+ *
14
+ **/
15
+
16
+ async function buildServer () {
17
+ const fastify = require('fastify')({ logger: true })
18
+
19
+ // Register the SSE plugin
20
+ await fastify.register(require('../index.js'))
21
+
22
+ // Basic SSE endpoint
23
+ fastify.get('/events', { sse: true }, async (request, reply) => {
24
+ // Send a simple message
25
+ await reply.sse.send({ data: 'Hello SSE!' })
26
+
27
+ // Send multiple events
28
+ for (let i = 0; i < 5; i++) {
29
+ await reply.sse.send({
30
+ id: String(i),
31
+ event: 'counter',
32
+ data: { count: i, timestamp: Date.now() }
33
+ })
34
+ }
21
35
 
22
- // Connection will close automatically when handler ends
23
- })
36
+ // Connection will close automatically when handler ends
37
+ })
24
38
 
25
- // Stream endpoint with async generator
26
- fastify.get('/stream', { sse: true }, async (request, reply) => {
27
- async function * generateEvents () {
28
- for (let i = 0; i < 10; i++) {
29
- await new Promise(resolve => setTimeout(resolve, 1000))
30
- yield {
31
- id: String(i),
32
- event: 'tick',
33
- data: { tick: i, time: new Date().toISOString() }
39
+ // Stream endpoint with async generator
40
+ fastify.get('/stream', { sse: true }, async (request, reply) => {
41
+ async function * generateEvents () {
42
+ for (let i = 0; i < 10; i++) {
43
+ await new Promise(resolve => setTimeout(resolve, 1000))
44
+ yield {
45
+ id: String(i),
46
+ event: 'tick',
47
+ data: { tick: i, time: new Date().toISOString() }
48
+ }
34
49
  }
35
50
  }
36
- }
37
51
 
38
- await reply.sse.send(generateEvents())
39
- })
40
-
41
- // Persistent connection with keepAlive
42
- fastify.get('/live', { sse: true }, async (request, reply) => {
43
- // Keep connection alive
44
- reply.sse.keepAlive()
45
-
46
- // Send initial event
47
- await reply.sse.send({ data: 'Connected to live stream' })
48
-
49
- // Send periodic updates
50
- const interval = setInterval(async () => {
51
- try {
52
- if (reply.sse.isConnected) {
53
- await reply.sse.send({
54
- event: 'heartbeat',
55
- data: { timestamp: Date.now() }
56
- })
57
- } else {
52
+ await reply.sse.send(generateEvents())
53
+ })
54
+
55
+ // Persistent connection with keepAlive
56
+ fastify.get('/live', { sse: true }, async (request, reply) => {
57
+ // Keep connection alive
58
+ reply.sse.keepAlive()
59
+
60
+ // Send initial event
61
+ await reply.sse.send({ data: 'Connected to live stream' })
62
+
63
+ // Send periodic updates
64
+ const interval = setInterval(async () => {
65
+ try {
66
+ if (reply.sse.isConnected) {
67
+ await reply.sse.send({
68
+ event: 'heartbeat',
69
+ data: { timestamp: Date.now() }
70
+ })
71
+ } else {
72
+ clearInterval(interval)
73
+ }
74
+ } catch (error) {
58
75
  clearInterval(interval)
59
76
  }
60
- } catch (error) {
61
- clearInterval(interval)
62
- }
63
- }, 2000)
77
+ }, 2000)
64
78
 
65
- // Clean up on close
66
- reply.sse.onClose(() => {
67
- clearInterval(interval)
68
- console.log('Live stream connection closed')
69
- })
70
- })
71
-
72
- // Custom headers with sendHeaders
73
- fastify.get('/headers', { sse: true }, async (request, reply) => {
74
- // Set custom headers using Fastify's header methods
75
- reply.header('X-Session-ID', 'session-12345')
76
- reply.header('X-API-Version', 'v1.2.3')
77
- reply.headers({
78
- 'X-User-Agent': request.headers['user-agent'] || 'unknown',
79
- 'X-Request-Time': new Date().toISOString()
79
+ // Clean up on close
80
+ reply.sse.onClose(() => {
81
+ clearInterval(interval)
82
+ console.log('Live stream connection closed')
83
+ })
80
84
  })
81
85
 
82
- // Manually send headers before any SSE data
83
- reply.sse.sendHeaders()
86
+ // Custom headers with sendHeaders
87
+ fastify.get('/headers', { sse: true }, async (request, reply) => {
88
+ // Set custom headers using Fastify's header methods
89
+ reply.header('X-Session-ID', 'session-12345')
90
+ reply.header('X-API-Version', 'v1.2.3')
91
+ reply.headers({
92
+ 'X-User-Agent': request.headers['user-agent'] || 'unknown',
93
+ 'X-Request-Time': new Date().toISOString()
94
+ })
95
+
96
+ // Manually send headers before any SSE data
97
+ reply.sse.sendHeaders()
84
98
 
85
- // Now send SSE data
86
- await reply.sse.send({
87
- data: 'Headers sent manually before this message',
88
- event: 'custom-headers'
99
+ // Now send SSE data
100
+ await reply.sse.send({
101
+ data: 'Headers sent manually before this message',
102
+ event: 'custom-headers'
103
+ })
89
104
  })
90
- })
91
-
92
- // Replay functionality
93
- const messageHistory = []
94
- let eventId = 0
95
-
96
- fastify.get('/replay', { sse: true }, async (request, reply) => {
97
- // Handle replay if client reconnects
98
- await reply.sse.replay(async (lastEventId) => {
99
- const startIndex = messageHistory.findIndex(msg => msg.id === lastEventId)
100
- const messagesToReplay = startIndex !== -1
101
- ? messageHistory.slice(startIndex + 1)
102
- : messageHistory
103
-
104
- for (const message of messagesToReplay) {
105
- await reply.sse.send(message)
105
+
106
+ // Replay functionality
107
+ const messageHistory = []
108
+ let eventId = 0
109
+
110
+ fastify.get('/replay', { sse: true }, async (request, reply) => {
111
+ // Handle replay if client reconnects
112
+
113
+ reply.sse.keepAlive()
114
+
115
+ await reply.sse.replay(async (lastEventId) => {
116
+ const startIndex = messageHistory.findIndex(msg => msg.id === lastEventId)
117
+ const messagesToReplay = startIndex !== -1
118
+ ? messageHistory.slice(startIndex + 1)
119
+ : messageHistory
120
+
121
+ for (const message of messagesToReplay) {
122
+ await reply.sse.send(message)
123
+ }
124
+ })
125
+
126
+ // Send new message
127
+ const newMessage = {
128
+ id: String(++eventId),
129
+ data: { message: `New event ${eventId}`, timestamp: Date.now() }
106
130
  }
107
- })
108
131
 
109
- // Send new message
110
- const newMessage = {
111
- id: String(++eventId),
112
- data: { message: `New event ${eventId}`, timestamp: Date.now() }
113
- }
132
+ messageHistory.push(newMessage)
114
133
 
115
- messageHistory.push(newMessage)
134
+ // Keep only last 100 messages
135
+ if (messageHistory.length > 100) {
136
+ messageHistory.shift()
137
+ }
116
138
 
117
- // Keep only last 100 messages
118
- if (messageHistory.length > 100) {
119
- messageHistory.shift()
120
- }
139
+ await reply.sse.send(newMessage)
140
+ })
121
141
 
122
- await reply.sse.send(newMessage)
123
- })
142
+ return fastify
143
+ }
124
144
 
125
145
  // Start the server
126
146
  const start = async () => {
147
+ const server = await buildServer()
127
148
  try {
128
- await fastify.listen({ port: 3000, host: '0.0.0.0' })
149
+ await server.listen({ port: 3000, host: '0.0.0.0' })
129
150
  console.log('Server listening on http://localhost:3000')
130
151
  console.log('Try these endpoints:')
131
152
  console.log(' GET /events - Basic SSE messages')
@@ -134,7 +155,7 @@ const start = async () => {
134
155
  console.log(' GET /headers - Custom headers with sendHeaders()')
135
156
  console.log(' GET /replay - Messages with replay support')
136
157
  } catch (err) {
137
- fastify.log.error(err)
158
+ server.log.error(err)
138
159
  process.exit(1)
139
160
  }
140
161
  }
@@ -0,0 +1,81 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * Dynamic streaming with @fastify/sse (`sse: 'manual'`)
5
+ *
6
+ * Some APIs — OpenAI-compatible and other LLM endpoints, for example —
7
+ * decide whether to stream based on the request *body* (`{ "stream": true }`)
8
+ * rather than the `Accept` header. `sse: 'manual'` skips Accept negotiation
9
+ * entirely: `reply.sse` is always attached and the handler decides at runtime
10
+ * whether to stream or to return a normal JSON response. The connection is
11
+ * closed automatically when the handler resolves or throws.
12
+ *
13
+ * Test endpoints with curl:
14
+ *
15
+ * # Streaming response (Server-Sent Events):
16
+ * curl -N -X POST http://localhost:3000/v1/chat/completions \
17
+ * -H "Content-Type: application/json" \
18
+ * -d '{"prompt":"hello","stream":true}'
19
+ *
20
+ * # Non-streaming response (single JSON object):
21
+ * curl -X POST http://localhost:3000/v1/chat/completions \
22
+ * -H "Content-Type: application/json" \
23
+ * -d '{"prompt":"hello","stream":false}'
24
+ *
25
+ **/
26
+
27
+ async function buildServer () {
28
+ const fastify = require('fastify')({ logger: true })
29
+
30
+ await fastify.register(require('../index.js'))
31
+
32
+ // A tiny stand-in for a model that emits tokens one at a time.
33
+ async function * generateTokens (prompt) {
34
+ const tokens = `Echo: ${prompt}`.split(' ')
35
+ for (let i = 0; i < tokens.length; i++) {
36
+ await new Promise(resolve => setTimeout(resolve, 200))
37
+ yield {
38
+ id: String(i),
39
+ event: 'token',
40
+ data: { index: i, token: tokens[i] }
41
+ }
42
+ }
43
+ }
44
+
45
+ fastify.post('/v1/chat/completions', { sse: 'manual' }, async (request, reply) => {
46
+ const { prompt = '', stream = false } = request.body ?? {}
47
+
48
+ if (stream) {
49
+ // Stream tokens as SSE. The SSE response headers are committed on the
50
+ // first send(); the connection closes when this handler resolves.
51
+ await reply.sse.send(generateTokens(prompt))
52
+ await reply.sse.send({ event: 'done', data: '[DONE]' })
53
+ return
54
+ }
55
+
56
+ // Non-streaming client — fall through to Fastify's normal JSON
57
+ // serialization. `reply.sse` was attached but never written to, so no
58
+ // SSE headers were sent.
59
+ return {
60
+ id: 'chatcmpl-1',
61
+ object: 'chat.completion',
62
+ choices: [{ message: { role: 'assistant', content: `Echo: ${prompt}` } }]
63
+ }
64
+ })
65
+
66
+ return fastify
67
+ }
68
+
69
+ const start = async () => {
70
+ const server = await buildServer()
71
+ try {
72
+ await server.listen({ port: 3000, host: '0.0.0.0' })
73
+ console.log('Server listening on http://localhost:3000')
74
+ console.log('Try POST /v1/chat/completions with {"stream": true} or {"stream": false}')
75
+ } catch (err) {
76
+ server.log.error(err)
77
+ process.exit(1)
78
+ }
79
+ }
80
+
81
+ start()