RejectedSoftware Forums

Sign up

"text/event-stream" in vibe-d, not sending the response to the client

Hi,

I'm trying to send updates to my client using an Event-Stream.

My code for updates is:

void statusUpdate(HTTPServerRequest req, HTTPServerResponse res){                                                                                            
    res.contentType = "text/event-stream";                                                                                                                   
    res.headers["Cache-Control"] = "no-cache";                                                                                                               
    import std.random : uniform;                                                                                                                             
    res.writeBody(format("This is a test...%s", uniform(1.0,1000.0)), "text/event-stream");                                                                  
}

And the code to handle it on the client is:

    script(type="text/javascript")                                                                                                                           
        if(!!window.EventSource){                                                                                                                            
            var source = new EventSource("/admin/statusUpdate");                                                                                             
            source.addEventListener('message', displayPoll, false);                                                                                          
            console.log("Listening to /admin/statusUpdate...");                                                                                              
            console.log("Source: " + source);                                                                                                                
                                                                                                                                                             
        }                                                                                                                                                    
                                                                                                                                                             
        function displayPoll(event){                                                                                                                         
                var html = event.data;                                                                                                                       
                var log = document.getElementById("serverStatus");                                                                                           
                if(log){                                                                                                                                     
                    log.innerHTML += "poll: " + html + "<br>";                                                                                               
                }                                                                                                                                            
            }

On the client, I can see that the requests are received every 3 or so seconds.
However, nothing is ever in the body of these requests, its just headers.

Also, the displayPoll function never seems to get called, I'm not sure as to why.
Maybe I'm constructing this thing all wrong?
Or maybe I'm using vibes res.writeBody() wrong?

Thanks for any help on this, been wrecking my brain on it all day!

Re: "text/event-stream" in vibe-d, not sending the response to the client

On Sun, 23 Feb 2014 17:54:02 GMT, Colin Grogan wrote:

Hi,

I'm trying to send updates to my client using an Event-Stream.

My code for updates is:

void statusUpdate(HTTPServerRequest req, HTTPServerResponse res){                                                                                            
    res.contentType = "text/event-stream";                                                                                                                   
    res.headers["Cache-Control"] = "no-cache";                                                                                                               
    import std.random : uniform;                                                                                                                             
    res.writeBody(format("This is a test...%s", uniform(1.0,1000.0)), "text/event-stream");                                                                  
}

And the code to handle it on the client is:

    script(type="text/javascript")                                                                                                                           
        if(!!window.EventSource){                                                                                                                            
            var source = new EventSource("/admin/statusUpdate");                                                                                             
            source.addEventListener('message', displayPoll, false);                                                                                          
            console.log("Listening to /admin/statusUpdate...");                                                                                              
            console.log("Source: " + source);                                                                                                                
                                                                                                                                                             
        }                                                                                                                                                    
                                                                                                                                                             
        function displayPoll(event){                                                                                                                         
                var html = event.data;                                                                                                                       
                var log = document.getElementById("serverStatus");                                                                                           
                if(log){                                                                                                                                     
                    log.innerHTML += "poll: " + html + "<br>";                                                                                               
                }                                                                                                                                            
            }

On the client, I can see that the requests are received every 3 or so seconds.
However, nothing is ever in the body of these requests, its just headers.

Also, the displayPoll function never seems to get called, I'm not sure as to why.
Maybe I'm constructing this thing all wrong?
Or maybe I'm using vibes res.writeBody() wrong?

Thanks for any help on this, been wrecking my brain on it all day!

Just to add, if I test this URL with curl, I get the expected responses.

$ curl localhost:26546/admin/statusUpdate
This is a test...345.004

So, it seems the issue is with how the browser is picking up the "event". Not sure whats going on...

Re: "text/event-stream" in vibe-d, not sending the response to the client

On Sun, 23 Feb 2014 22:15:14 GMT, Colin Grogan wrote:

On Sun, 23 Feb 2014 17:54:02 GMT, Colin Grogan wrote:

Hi,

I'm trying to send updates to my client using an Event-Stream.

My code for updates is:

void statusUpdate(HTTPServerRequest req, HTTPServerResponse res){                                                                                            
    res.contentType = "text/event-stream";                                                                                                                   
    res.headers["Cache-Control"] = "no-cache";                                                                                                               
    import std.random : uniform;                                                                                                                             
    res.writeBody(format("This is a test...%s", uniform(1.0,1000.0)), "text/event-stream");                                                                  
}

And the code to handle it on the client is:

    script(type="text/javascript")                                                                                                                           
        if(!!window.EventSource){                                                                                                                            
            var source = new EventSource("/admin/statusUpdate");                                                                                             
            source.addEventListener('message', displayPoll, false);                                                                                          
            console.log("Listening to /admin/statusUpdate...");                                                                                              
            console.log("Source: " + source);                                                                                                                
                                                                                                                                                             
        }                                                                                                                                                    
                                                                                                                                                             
        function displayPoll(event){                                                                                                                         
                var html = event.data;                                                                                                                       
                var log = document.getElementById("serverStatus");                                                                                           
                if(log){                                                                                                                                     
                    log.innerHTML += "poll: " + html + "<br>";                                                                                               
                }                                                                                                                                            
            }

On the client, I can see that the requests are received every 3 or so seconds.
However, nothing is ever in the body of these requests, its just headers.

Also, the displayPoll function never seems to get called, I'm not sure as to why.
Maybe I'm constructing this thing all wrong?
Or maybe I'm using vibes res.writeBody() wrong?

Thanks for any help on this, been wrecking my brain on it all day!

Just to add, if I test this URL with curl, I get the expected responses.

$ curl localhost:26546/admin/statusUpdate
This is a test...345.004

So, it seems the issue is with how the browser is picking up the "event". Not sure whats going on...

Never used the EventSource interface yet, but judging by its documentation ( https://developer.mozilla.org/en-US/docs/Server-sentevents/Usingserver-sent_events ) you are supposed to send json data from the server and the event interface looks for an "event" field in there to route the message to the correct event listener (in your case the "message" event)

Re: "text/event-stream" in vibe-d, not sending the response to the client

On Sun, 23 Feb 2014 22:35:02 GMT, Stephan Dilly wrote:

Never used the EventSource interface yet, but judging by its documentation ( https://developer.mozilla.org/en-US/docs/Server-sentevents/Usingserver-sent_events ) you are supposed to send json data from the server and the event interface looks for an "event" field in there to route the message to the correct event listener (in your case the "message" event)

Even more weird it expects the server to send something like this:

event: message
data: {"json":"payload"}

just look at the php example the give under the link above

Re: "text/event-stream" in vibe-d, not sending the response to the client

On Sun, 23 Feb 2014 22:37:19 GMT, Stephan Dilly wrote:

On Sun, 23 Feb 2014 22:35:02 GMT, Stephan Dilly wrote:

Never used the EventSource interface yet, but judging by its documentation ( https://developer.mozilla.org/en-US/docs/Server-sentevents/Usingserver-sent_events ) you are supposed to send json data from the server and the event interface looks for an "event" field in there to route the message to the correct event listener (in your case the "message" event)

Even more weird it expects the server to send something like this:

event: message
data: {"json":"payload"}

just look at the php example the give under the link above

Ah, your right. I'll do a bit of extra reading on this I guess. From the tutorials I read I thought I was taking the right approach, but that link states otherwise. Thanks