How does one reuse a web socket client

217 views
Skip to first unread message

chri...@gmail.com

unread,
Aug 5, 2017, 7:49:31 AM8/5/17
to vert.x
 The Vert.x HttpClient supports WebSockets. I've got this example (in Kotlin) of creating a web socket and then setting up a handler to listen to data coming in via the socket. In this case the data is sent to an address on the event bus ready to be picked up and processed elsewhere.

client.websocket("/some-uri", websocket -> {
  websocket
        .handler({ data ->
            vertx.eventBus().send("some-address", data.toJsonObject())
        })
});

I see this as something of a one-time activity which sets up a websocket and keeps it open to receiving data from the server.

My uncertainty comes in the situation where I want to write to the websocket. I could do something like this:

client.websocket("/some-uri", websocket -> {
  websocket.writeTextMessage("A message from the client to the server")
});

However this looks as though it will create a new websocket each time I execute it; not very efficient. Is there a recommended way to "reuse" a websocket?


Julien Viet

unread,
Aug 5, 2017, 2:35:15 PM8/5/17
to ve...@googlegroups.com
Hi,

I think you should have in mind that you are developing an event driven application, so when you get the websocket event you need to setup the various event handler your application will react to, here are a few examples:

1/ send message periodically (timer event)

websocket -> {
  vertx.setPeriodic(1000, id -> {
    websocket.writeTextMessage(“a message");
  });
}

2/ react to an event bus message

websocket -> {
  vertx.eventBus().handler(“the-address”, msg -> {
    websocket.writeTextMessage(“a message");
  });
}

keep in mind also to unregister handlers when the websocket closes, for instance:

1/

websocket -> {
  long timerID = vertx.setPeriodic(1000, id -> { … });
  websocket.closeHandler(v -> {
    vertx.cancelTimer(timerID);
  });
}

2/

websocket -> {
  MessageConsumer<String> consumer = vertx.eventBus().handler(“the-address”, msg -> {
    //
  });
  websocket.closeHandler(v -> {
    consumer.unregister();
  });
}

I hope it will help you to get you started with event driven application.

I recommend also to read the guide to application development with Vert.x although it is in Java : http://vertx.io/docs/guide-for-java-devs/

cheers

Julien

--
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.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/c310a45a-6ab7-4f1b-b5fe-f6b5c98fba26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

chri...@gmail.com

unread,
Aug 5, 2017, 5:56:46 PM8/5/17
to vert.x
This was really helpful thanks Julien. Example 2 is exactly what I was looking for; seems simple now that I see it. I now have something like this:

client.websocket("/some-uri",
{ websocket ->

val handlerA = vertx.eventBus().consumer<JsonObject>("address:a").handler({ message ->
websocket.writeTextMessage(message.body().encode())
})

val handlerB = vertx.eventBus().consumer<JsonObject>("address:b").handler({ message ->
websocket.writeTextMessage(message.body().encode())
})

websocket
// Handle incoming messages from server
.handler({ data ->
LOGGER.debug("Got message '{}'", data.toJsonObject())
vertx.eventBus().send(did, data.toJsonObject())
})
// Clean up event bus handlers
.closeHandler {
handlerA.unregister()
handlerB.unregister()
}
},
{ failure ->
// ...
})
 
Thanks for the tip about unregistering too. Just to confirm though, is unregistering the handler as I'm doing sufficient or must it be done on the actual MessageConsumer?
Reply all
Reply to author
Forward
0 new messages