Closed zopf closed 6 years ago
I've never used server sent events on Koa; perhaps you can use your knowledge of Koa to help us figure out the issue?
Here's where requestHandler
triggers the server sent events:
And here's the server sent events file:
https://github.com/postgraphql/postgraphql/blob/master/src/postgraphql/http/setupServerSentEvents.js
And here's where we sort of hack Koa support:
It sounds like, unsurprisingly, Koa doesn't trigger the close
event: https://github.com/postgraphql/postgraphql/blob/master/src/postgraphql/http/setupServerSentEvents.js#L34-L37
Does koa-connect solve this issue?
I think what's needed is a separate middleware for koa, the compatibility isn't working 100%.
When I run
postgraphql
as middleware in my Koa app, I frequently see a warning message like:MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 schemas:changed listeners added. Use emitter.setMaxListeners() to increase limit
The warning usually appears after perhaps 5-30 seconds of node execution.
After digging in, I realized that it must be that many requests were being made to
setupServerSentEvents
without properly unsubscribing from theschemas:changed
event type. (link)Looking at the browser-server traffic, I'm seeing that when I open
http://localhost:3000/graphiql
in Chrome Version 59.0.3071.115 (Official Build) (64-bit),PostgraphiQL.js
seems to be trying and failing to make a kept-alive event emitter connection to the server here, and then repeating this every couple seconds. Instead of maintaining a persistent event listener connection, it is opening and immediately erroring one, apparently, although when I look in the Network tab of the Chrome inspector, I see a200 OK
response to the request tohttp://localhost:3000/_postgraphql/stream
. The connection stays open for only about 4ms.Here's the request headers:
And here's the request response:
For clarity, here is the code I'm using to set up
postgraphql
as middleware:Is there anything special that I need to do to get the server-side events listener to work with Koa when using
postgraphql
as middleware?