# Websocket client add element to source dynamically

**URL:** <https://discuss.akka.io/t/websocket-client-add-element-to-source-dynamically/7070>\
**Category:** Akka HTTP\
**Tags:** akka-http, websockets\
**Created:** [August 26, 2020, 8:44am UTC](https://discuss.akka.io/t/websocket-client-add-element-to-source-dynamically/7070 "2020-08-26T08:44:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brabo-hi](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/brabo-hi/32/1460_2.png) [@brabo-hi](https://discuss.akka.io/u/brabo-hi)\
**Post date:** [August 26, 2020, 8:44am UTC](https://discuss.akka.io/t/websocket-client-add-element-to-source-dynamically/7070/1 "2020-08-26T08:44:14Z")

</div>

Hi all,  
i would like to keep multiple websocket client connections, and for each connection i will later send message, what would be the right way of implementing it?  
i have actors sharded, and in each of them, i have a websocket connection, like bellow

```auto
val request = WebSocketRequest()
val websocketFlow = Http()(classicSystem).webSocketClientFlow(request)
val (((queue, upgradeResponse)), closed) =
      Source.queue[TextMessage](bufferSize = 10, OverflowStrategy.backpressure)
        .viaMat(websocketFlow)(Keep.both)
        .toMat(incoming)(Keep.both)
        .run()(materializer)

```

And i am using later

> queue.offer()

to send new message  
It works when there is one connection, however whenever i start a new connection (on another actor sharded), the first actor’s queue just closes

---

<div class="post-metadata">

**Author:** ![jrudolph](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/jrudolph/32/80_2.png) [@jrudolph](https://discuss.akka.io/u/jrudolph)\
**Post date:** [September 14, 2020, 12:15pm UTC](https://discuss.akka.io/t/websocket-client-add-element-to-source-dynamically/7070/2 "2020-09-14T12:15:41Z")

</div>

Hi @brabo-hi,

not clear why that would happen. Do you share anything between those actors? Maybe the `incoming` stream part?

Johannes

---

<div class="post-metadata">

**Author:** ![brabo-hi](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/brabo-hi/32/1460_2.png) [@brabo-hi](https://discuss.akka.io/u/brabo-hi)\
**Post date:** [September 15, 2020, 1:35am UTC](https://discuss.akka.io/t/websocket-client-add-element-to-source-dynamically/7070/3 "2020-09-15T01:35:37Z")

</div>

Hello @jrudolph

They don’t share anything. They are represented as a persistent actor.  
I have a lagom service. For each request, I send a message to a persistent actor shared.

```auto
def entityRef(entityId: String): EntityRef[PeerEntity.Command] = clusterSharding.entityRefFor(PeerEntity.typeKey, entityId)

```

Inside each actor I start a websocket connection to an external websocket server  
The issue I am having is :  
The websocket is alive only in 1 actor.  
When I receive another request and forward it to a new actor (that will start it own) websocket connection, the first actor drops it connection.  
Which means at any give time I only have one actor with one websoket connection
