interfaces.eventStream.connect

Hi having issue trying to get eventSteam to work

the incoming request from the Hub looks like

GET /stream HTTP/1.1
Host: localhost:8081
User-Agent: okhttp/3.14.9
Accept: text/event-stream
Accept-Encoding: gzip

and the response

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/event-stream
Connection: keep-alive
Cache-Control: no-cache
Date: Thu, 13 Aug 2020 23:32:42 GMT
Transfer-Encoding: chunked

every couple of seconds it prints

data: {"event":1}

I can see it steam in the browser but the HUB seems to be forever connected.

in the log I see this

dev:652020-08-14 00:40:03.956 warnstatus2
dev:652020-08-14 00:40:03.952 warnSTART: EventStream Started
dev:652020-08-14 00:40:03.948 warnstatus

but thats it.

my driver


definition (name: "Testing", namespace: "local-test", author: "test") {
  command "streamStart", null
  command "streamStop", null
}

void initialize(){
}

void parse(String description) {
  log.warn("hi from parse")
  log.warn(description)
}

void uninstalled() {
}

def eventStreamStatus(String msg) {
    log.warn "status"
    log.warn msg
    log.warn "status2"
}

void streamStart() {
    log.warn "streamStart"
    try {
      interfaces.eventStream.connect(
        "http://my-host/stream",
        [pingInterval: 5, headers:[Accept: "text/event-stream"]]
      
      )
    } catch(e) {
      log.warn "error"
      log.warn e   
   }
}

void streamStop() {
    log.warn "streamStop"
    interfaces.eventStream.close()
}

Am I missing something? thank you

As I understand it, event stream is just like a websocket connection. It stays open until you close it.
https://docs.hubitat.com/index.php?title=EventStream_Interface

yep I understand that thanks, its just not clear what kind of response it wants from the server.

with response type Content-Type: text/event-stream I've created a simple counter that counts up every second now.

1
2
3
4

I can see that hubitat is connected to my endpoint and waiting for a response it seem but parse never prints anything out.

I can get working using getInputStream method from groovy URL though.

Are you able to connect to the event stream from another system other than hubitat?

hmm yep, I don't think it's any kinda network issue. I can connect to the endpoint from multiple computers and through ngrok.io to test.

my groovy code is running on hubitat and connects to the endpoint fine using the below and prints out a response every second.

def url = "localhost:4000/stream".toURL()
url.openConnection();
def stream = connection.getInputStream()
        
stream.eachLine { text ->
  log.debug text
}

however this does not work, along side eventStreamStatus

interfaces.eventStream.connect(
  "http://localhost:4000/stream",
  [pingInterval: 5, headers:[Accept: "text/event-stream"]]
)

it prints

START: EventStream Started during connection

and when interfaces.eventStream.close() is called.

STOP: EventStream Stopped

otherwise there is no other output even though stream.eachLine continues to print.

Download the Hubitat app