rexxars / sse-channel

Server-Sent Events "channel" where all messages are broadcasted to all connected clients, history is maintained automatically and server attempts to keep clients alive by sending "keep-alive" packets automatically.
MIT License
111 stars 11 forks source link

Events aren't appearing in the browser #8

Closed mark-hahn closed 8 years ago

mark-hahn commented 8 years ago

I'm having problems getting events to come through. I think I have everything set up (but I am often wrong).

Server code ...

channel = new SseChannel
  historySize: 10
  retryTimeout: 250
  pingInterval: 5000
  jsonEncode: yes

--- snip ---

  when '/channel'
    res.setHeader 'Transfer-Encoding', 'identity'
    channel.addClient req, res, (err) ->
      if err then log 'channel.addClient err:', err.message

--- snip ---
do sendStatus = ->
  vlc.status (err, status) ->
    if err then log 'sendStatus err: ', err
    else
      log 'channel.send', channel.getConnectionCount(), status
      channel.send 
        event: 'status'
        id:     Date.now()
        data:   status
        retry:  5000
    setTimeout sendStatus, 2000

Client code ...

    evtSource = new EventSource "http://#{tvGlobal.serverIp}:2340/channel"
    log 'evtSource', evtSource

    evtSource.onopen = (e) ->
      log 'evtSource.onopen1:', e

    evtSource.onmessage = (e) ->
      log 'evtSource.onmessage:', e

    evtSource.onerror = (e) ->
      log 'evtSource.error:', e

    evtSource.addEventListener 'open', (e) ->
      log 'evtSource.onopen2:', e

    evtSource.addEventListener 'message', (e) ->
      log 'evtSource.onmessage:', e

    evtSource.addEventListener 'error', (e) ->
      log 'evtSource.error:', e

    setInterval ->
      log 'evtSource', evtSource
    , 5000

If you can't think of any easier way to debug I'll try capturing packets.

Ubuntu 14.04 node 0.12.9 sse-channel 2.0.0 chrome 47.0.2526.106

rexxars commented 8 years ago

Couple of things:

If you're still having trouble getting it to work, try using curl and checking that messages are actually being broadcasted: curl http://localhost:2340/channel

mark-hahn commented 8 years ago

hook up an event listener on status

That fixed it. Thanks very much. Maybe this should be in the readme. Maybe a short section on what to do in the browser. The readme currently assumes the user knows SSE in advance. I stupidly looked for a module to run in the browser until I found out it wasn't needed. If I get a chance I'll do a PR.

SSE should use chunked

Chrome threw an error saying chunked can't be used. I also googled it and found that chunked is considered a relibility problem.

"Authors are also cautioned that HTTP chunking can have unexpected negative effects on the reliability of this protocol. Where possible, chunking should be disabled for serving event streams unless the rate of messages is high enough for this not to matter." From https://html.spec.whatwg.org/multipage/comms.html#authoring-notes

Edit: Weird. I commented out the identity header and had no problem. Apparently that error was triggered by my problem, which makes no sense. I didn't imagine the error.

Thanks again for the fast response.

rexxars commented 8 years ago

Thanks for the feedback on the transfer-encoding, learn something new every day :-)

I'll see if I can find the time to make a few notes in the readme about browser usage.

rexxars commented 8 years ago

Thanks again for the feedback. I've added some client usage notes to the readme: https://github.com/rexxars/sse-channel/commit/6d6f2cac8590b9aedec763d7afdaa97c12ee7379