You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Mark <el...@gmail.com> on 2016/10/18 14:51:06 UTC

send same message to multiple, not all, websocket clients

I am receiving messages of data that I am processing with Camel, similar to
a chat system.  Clients may "subscribe" to the data feed which will require
me to send the messages to the clients that subscribe to that message type
over websockets.  How can I implement this websocket communication using
Camel since the current implementation only allows for one connection key
per message?  Would I somehow duplicate the message and set a different
connection key for each message?

Re: send same message to multiple, not all, websocket clients

Posted by Mark <el...@gmail.com>.
Thanks for your help.  I am maintaining a Map<String,List<String>> of
client information.  I set the value of the map to the exchange header and
then send the message to a Splitter.  The splitter will split on the
exchange header and then I can send out the messages for each connection
key. Its working great.



On Wed, Oct 19, 2016 at 1:20 AM, <sh...@bdbizviz.com> wrote:

> Hi Mark,
>         you have to keep connection keys from each client in a map or list,
> then you need to send same data to the producer with different connection
> key.
>
> here is how you can get the connection key from each client. whenever a
> client makes a connection with your websocket consumer you will get a
> connection key.
>
> Apache camel websocket consumer example.
>      from("direct:Consumer1")
>     .process(new Processor() {
>      public void process(Exchange exchange) throws Exception {
>        Map<String, Object> headers=exchange.getIn().getHeaders();
>     //you will get a unique connection key from the exchange header.This
> would be unique for each client.
>     //store this key somewhere, to send messages to particular client.
>     String uniqueConnectionKey=headers.get("websocket.connectionKey")
> .toString();
>           //you can get message from the client like below.
>           String dataFromClient=exchange.getIn().getBody().toString();
>
>    }
> }).end();
>
> Apache camel websocket producer example:
>       from("direct:Producer1").
>       //we will use this connectionKey for uniquely identifying each
> connection from the client.
>       setHeader(WebsocketConstants.CONNECTION_KEY,
> header("connectionKey")).
>       to("websocket://{host}:{port}/camel-websocket?sendToAll=
> false").end();
>
> here is the sample for producer template.
> ProducerTemplate template=camelContext.createProducerTemplate();
>
> repeat this step with same message and different connection key:
> template.sendBodyAndHeader("direct:Producer1", {message},
> "connectionKey", {connectionkey});
>
> I hope this would help
>
> -----Original Message-----
> From: "Mark" <el...@gmail.com>
> Sent: Tuesday, October 18, 2016 10:51am
> To: users@camel.apache.org
> Subject: send same message to multiple, not all, websocket clients
>
> I am receiving messages of data that I am processing with Camel, similar to
> a chat system.  Clients may "subscribe" to the data feed which will require
> me to send the messages to the clients that subscribe to that message type
> over websockets.  How can I implement this websocket communication using
> Camel since the current implementation only allows for one connection key
> per message?  Would I somehow duplicate the message and set a different
> connection key for each message?
>
>
>

RE: send same message to multiple, not all, websocket clients

Posted by sh...@bdbizviz.com.
Hi Mark,
        you have to keep connection keys from each client in a map or list,
then you need to send same data to the producer with different connection key.

here is how you can get the connection key from each client. whenever a client makes a connection with your websocket consumer you will get a connection key.

Apache camel websocket consumer example.
     from("direct:Consumer1")
    .process(new Processor() {
     public void process(Exchange exchange) throws Exception {
       Map<String, Object> headers=exchange.getIn().getHeaders();
    //you will get a unique connection key from the exchange header.This would be unique for each client.
    //store this key somewhere, to send messages to particular client.
    String uniqueConnectionKey=headers.get("websocket.connectionKey").toString();
          //you can get message from the client like below.
          String dataFromClient=exchange.getIn().getBody().toString();
  
   }
}).end();

Apache camel websocket producer example:
      from("direct:Producer1").
      //we will use this connectionKey for uniquely identifying each connection from the client.
      setHeader(WebsocketConstants.CONNECTION_KEY, header("connectionKey")).
      to("websocket://{host}:{port}/camel-websocket?sendToAll=false").end();

here is the sample for producer template.
ProducerTemplate template=camelContext.createProducerTemplate();

repeat this step with same message and different connection key:
template.sendBodyAndHeader("direct:Producer1", {message}, "connectionKey", {connectionkey});

I hope this would help

-----Original Message-----
From: "Mark" <el...@gmail.com>
Sent: Tuesday, October 18, 2016 10:51am
To: users@camel.apache.org
Subject: send same message to multiple, not all, websocket clients

I am receiving messages of data that I am processing with Camel, similar to
a chat system.  Clients may "subscribe" to the data feed which will require
me to send the messages to the clients that subscribe to that message type
over websockets.  How can I implement this websocket communication using
Camel since the current implementation only allows for one connection key
per message?  Would I somehow duplicate the message and set a different
connection key for each message?



Re: send same message to multiple, not all, websocket clients

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

So ootb camel-websocket is either point to point or pub/sub. So if you
need to send a point to point to more than one, but not all, then I
think you need to make your own house keeping of those connection keys
you want to send to, and then send to them one by one.



On Tue, Oct 18, 2016 at 4:51 PM, Mark <el...@gmail.com> wrote:
> I am receiving messages of data that I am processing with Camel, similar to
> a chat system.  Clients may "subscribe" to the data feed which will require
> me to send the messages to the clients that subscribe to that message type
> over websockets.  How can I implement this websocket communication using
> Camel since the current implementation only allows for one connection key
> per message?  Would I somehow duplicate the message and set a different
> connection key for each message?



-- 
Claus Ibsen
-----------------
http://davsclaus.com @davsclaus
Camel in Action 2: https://www.manning.com/ibsen2