lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.21k stars 85 forks source link

Support for Server Sent Events #68

Closed djKianoosh closed 7 years ago

djKianoosh commented 7 years ago

Does the new LWS support eventsource connections to backend services? The previous version was working fine. On version

$ ws --version
2.0.0-pre.4

eventsource (aka Server Sent Events with accept: 'text/event-stream',) seem to always return empty responses. When using -v to see the output, the data field is empty (see all the way at the end)...

{ 'socket-data': { id: 5, bytesRead: 730, bytesWritten: 0 } }
{ 'rewrite-proxy-req':
   { req:
      Url {
        protocol: 'http:',
        slashes: true,
        auth: null,
        host: '**myhostname**:8080',
        port: '8080',
        hostname: '**myhostname**',
        hash: null,
        search: '?&streamId=13d783492c76a9be5fbb34bdaeb936c9691fd893fd67a11ed99051c55c855259',
        query: '&streamId=13d783492c76a9be5fbb34bdaeb936c9691fd893fd67a11ed99051c55c855259',
        pathname: '/path/to/stream',
        path: '/path/to/stream?&streamId=13d783492c76a9be5fbb34bdaeb936c9691fd893fd67a11ed99051c55c855259',
        href: 'http://**myhostname**:8080/path/to/stream?&streamId=13d783492c76a9be5fbb34bdaeb936c9691fd893fd67a11ed99051c55c855259',
        id: 3,
        method: 'GET',
        headers:
         { host: '**myhostname**:8080',
           connection: 'keep-alive',
           accept: 'text/event-stream',
           'cache-control': 'no-cache',
           'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
           dnt: '1',
           referer: 'http://**myhostname**/path/to/app/',
           'accept-encoding': 'gzip, deflate',
           'accept-language': 'en-US,en;q=0.8',
           cookie: 'JSESSIONID=15A2;and_other_cookies..' },
        rejectUnauthorized: false },
     data: '' } }

See the last line has empty data: data: ''. But when I hit my downstream service directly I get periodic events as expected. Is there a new middleware I need to configure/enable to turn on SSE for rewrite requests?

I haven't tried websockets so it may or may not behave the same way.

75lb commented 7 years ago

hi.. will look into this now.. does it work with the latest version (2.0.0-pre2.3)? Your version is a little old now..

npm install -g local-web-server@next
75lb commented 7 years ago

could you send me a simple test case please to help me reproduce?

djKianoosh commented 7 years ago

still the same issue with 2.0.0-pre2.3. I'll see if I can make something later tonight that you can use. My real backend is a Java service that implements SSE over servlets, but any service that implements the SSE spec should work. Basically something like a simple express.js server that pushes some data on an interval like one of these:

https://github.com/fmvilas/sse/blob/develop/examples/server.js (probably the simplest example) https://github.com/trquoccuong/Server-Sent-Events-Nodejs

Some other links I just dug (for the curious, and for reference later)... https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events https://github.com/schwarzkopfb/sse-broadcast http://bayn.es/real-time-web-apps-with-server-sent-events-pt-2/ https://technology.amis.nl/2017/02/19/node-js-application-using-sse-server-sent-events-to-push-updates-read-from-kafka-topic-to-simple-html-client-application/

75lb commented 7 years ago

thanks, looking into it now..

75lb commented 7 years ago

this is fixed in v2.0.0-pre2.4, please reinstall

npm install -g local-web-server@next

thanks again, let me know if you find anything else!

djKianoosh commented 7 years ago

Fantastic. Works great... now -v verbose output shows:

        statusCode: 200,
        statusMessage: 'OK',
        url: '' },
     data: [ SSE event-stream ] } }

👍