Skip to content

Commit aa46eb8

Browse files
committed
fix: make ping conform with the spec
1 parent 9d9ef2a commit aa46eb8

File tree

3 files changed

+9
-13
lines changed

3 files changed

+9
-13
lines changed

src/transports/http/server.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { logger } from "../../core/Logger.js";
2121
import { getRequestHeader, setResponseHeaders } from "../../utils/headers.js";
2222
import { DEFAULT_CORS_CONFIG } from "../sse/types.js";
2323
import { CORSConfig } from "../sse/types.js";
24+
import { PING_SSE_MESSAGE } from "../utils/ping-message.js";
2425

2526
function isRequest(msg: JsonRpcMessage): msg is JsonRpcRequest {
2627
return msg &&
@@ -645,8 +646,7 @@ export class HttpStreamTransport extends AbstractTransport {
645646
private sendPing(connection: ActiveSseConnection): void {
646647
if (!connection || !connection.res || connection.res.writableEnded) return;
647648
try {
648-
const pingMessage = { jsonrpc: "2.0", method: "ping", params: { timestamp: Date.now() } };
649-
connection.res.write(`data: ${JSON.stringify(pingMessage)}\n\n`);
649+
connection.res.write(PING_SSE_MESSAGE);
650650
logger.debug(`Sent keep-alive ping to stream ${connection.streamId}`);
651651
} catch (error: any) {
652652
logger.error(`Error sending ping to stream ${connection.streamId}: ${error.message}`);

src/transports/sse/server.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { AbstractTransport } from "../base.js"
99
import { DEFAULT_SSE_CONFIG, SSETransportConfig, SSETransportConfigInternal, DEFAULT_CORS_CONFIG, CORSConfig } from "./types.js"
1010
import { logger } from "../../core/Logger.js"
1111
import { getRequestHeader, setResponseHeaders } from "../../utils/headers.js"
12+
import { PING_SSE_MESSAGE } from "../utils/ping-message.js";
1213

1314
interface ExtendedIncomingMessage extends IncomingMessage {
1415
body?: ClientRequest
@@ -225,21 +226,11 @@ export class SSEServerTransport extends AbstractTransport {
225226
res.write(`event: endpoint\ndata: ${endpointUrl}\n\n`)
226227

227228
logger.debug('Sending initial keep-alive')
228-
// Removed initial keep-alive: res.write(": keep-alive\n\n")
229229

230230
this._keepAliveInterval = setInterval(() => {
231231
if (this._sseResponse && !this._sseResponse.writableEnded) {
232232
try {
233-
// Sending proper JSON-RPC ping instead of ': keep-alive' comment
234-
// logger.debug('Sending keep-alive ping')
235-
// this._sseResponse.write(": keep-alive\n\n")
236-
237-
const pingMessage = {
238-
jsonrpc: "2.0",
239-
method: "ping",
240-
params: { timestamp: Date.now() }
241-
}
242-
this._sseResponse.write(`data: ${JSON.stringify(pingMessage)}\n\n`)
233+
this._sseResponse.write(PING_SSE_MESSAGE);
243234
} catch (error) {
244235
logger.error(`Error sending keep-alive: ${error}`)
245236
this.cleanupConnection()

src/transports/utils/ping-message.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Pre-stringified JSON-RPC ping message formatted for Server-Sent Events (SSE).
3+
* Includes the 'data: ' prefix and trailing newlines.
4+
*/
5+
export const PING_SSE_MESSAGE = 'data: {"jsonrpc":"2.0","method":"ping"}\n\n';

0 commit comments

Comments
 (0)