Server Sent Events and vert.x

1,110 views
Skip to first unread message

Christoph Grotz

unread,
Jun 3, 2015, 6:44:47 AM6/3/15
to ve...@googlegroups.com
Hi,

I tried doing SSE with vert.x and found it uncomfortable:

vertx.createHttpServer().requestHandler(request -> {

final HttpServerResponse response = request.response();

response.setChunked(true);

response.headers().add("Content-Type", "text/event-stream;charset=UTF-8");
response.headers().add("Connection", "keep-alive");

vertx.eventBus().registerHandler("events", msg -> {
response.write("event: message\ndata: " + msg.body() + "\n\n");
});

}).listen(8080);

The problem seems to be, that the response isn't flushed to the browser. Therefore the only possiblity to have a successful SSE is by occassionally closing the connection.

My inital thought was that I'm probably missing something.

Could somebody please help.

Regrads,
Christoph

Arnaud Estève

unread,
Jun 3, 2015, 8:21:42 AM6/3/15
to ve...@googlegroups.com
I'm trying to implement an SSE "official" implementation I'll submit to vertx-web once 3.0 is out.

You can find the source code here. (btw what you're writing is the "forward" method I wrote on SSEConnection).

And it works in my browser (in both Chrome & Firefox) and I don't need to close the connection, I simply write into it the same way you do.


Here's the code of my handler, maybe if you try to write the same kind of things it would work ? ("no cache" ? would be weird, but...)


Anyway, I hope this implementation can help you. 

Tim Fox

unread,
Jun 3, 2015, 8:23:17 AM6/3/15
to ve...@googlegroups.com
On 03/06/15 11:44, Christoph Grotz wrote:
Hi,

I tried doing SSE with vert.x and found it uncomfortable:

vertx.createHttpServer().requestHandler(request -> {

    final HttpServerResponse response = request.response();

    response.setChunked(true);

    response.headers().add("Content-Type", "text/event-stream;charset=UTF-8");
    response.headers().add("Connection", "keep-alive");

    vertx.eventBus().registerHandler("events", msg -> {
        response.write("event: message\ndata: " + msg.body() + "\n\n");
    });

}).listen(8080);

The problem seems to be, that the response isn't flushed to the browser.

It certainly should be. Can you provide a reproducer?

 Therefore the only possiblity to have a successful SSE is by occassionally closing the connection.

My inital thought was that I'm probably missing something.

Could somebody please help.

Regrads,
Christoph
--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Christoph Grotz

unread,
Jun 4, 2015, 11:52:35 AM6/4/15
to ve...@googlegroups.com
Hi,

I'm not able to reproduce the problem with vert.x 3.0.0-milestone6 under ubuntu.

I will test it under windows again, to check if it's plaftorm issue.

I created a sample project here for testing: https://github.com/cgrotz/vertx-sse

Regards,
Christoph
Message has been deleted

Mario Macías

unread,
Apr 1, 2016, 11:26:28 AM4/1/16
to vert.x
Hi all,

I have just released version 0.10.0 of jEaSSE (Java Easy Sse). Integrating it in your example be as simple as:

vertx.createHttpServer().requestHandler(request -> {

EventTarget target = new VertXEventTarget(request);
    vertx.eventBus().registerHandler("events", msg -> {
        target.("message", msg.body());
});

}).listen(8080);

I'm open for comments and suggestions.

Cheers.

Clement Escoffier

unread,
Apr 1, 2016, 11:30:25 AM4/1/16
to ve...@googlegroups.com
Hi,

There is probably a typo in target.(“message…) but it looks great ! 

Are you listed on vertx-awesome ? If not, submit a PR.

Clement

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Mario Macías

unread,
Apr 1, 2016, 11:41:12 AM4/1/16
to vert.x
You are right. It is target.send("message","blahblah").

No, I'm not listed in vertx-awesome... I just started coding in vertx three days ago :-D
Reply all
Reply to author
Forward
0 new messages