# Websocket Reconnect/Retry - Is it Possible

**URL:** <https://discuss.akka.io/t/websocket-reconnect-retry-is-it-possible/9676>\
**Category:** Akka HTTP\
**Tags:** akka-http, websockets\
**Created:** [April 7, 2022, 1:45am UTC](https://discuss.akka.io/t/websocket-reconnect-retry-is-it-possible/9676 "2022-04-07T01:45:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattmar10](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/mattmar10/32/2838_2.png) [@mattmar10](https://discuss.akka.io/u/mattmar10)\
**Post date:** [April 7, 2022, 1:45am UTC](https://discuss.akka.io/t/websocket-reconnect-retry-is-it-possible/9676/1 "2022-04-07T01:45:04Z")

</div>

Hi all,

I’m getting started with akka http websockets (from the client side), and I’ve encountered a scenario where the websocket opens and data flows… until it doesn’t. I’m not sure why but occasionally the websocket terminates. Is there a proper way to have akka http try to reconnect the websocket if it fails?

_Note, this is from within an actor who’s state is the list of other actors to send data as it arrives_

This is the code I have for setting up the websocket.

Any tips/suggestions are greatly appreciated.

```auto
       val incoming: Sink[Message, Future[Done]] =
         Sink.foreach[Message] {
           case message: TextMessage.Strict =>
             handleIncomingWSMessage(message.text)
           case _ =>
           // ignore other message types
         }

       val subscribeData =
         """{"action": "subscribe", "params": { "symbols": "QQQ" } }""".stripMargin

       val subscribeSource = Source(List(TextMessage(subscribeData)))
       //this sends one message on the outgoing websocket to subscribe, then waits
       val flow: Flow[Message, Message, Promise[Option[Message]]] =
         Flow.fromSinkAndSourceMat(
           incoming,
           subscribeSource.concatMat(Source.maybe[Message])(Keep.right)
         )(Keep.right)

       val (upgradeResponse, closed) =
         Http(context.system).singleWebSocketRequest(
           WebSocketRequest(
             s"wss://ws.twelvedata.com/v1/quotes/price?apikey=$apiKey"
           ),
           flow,
           settings = getClientConnectionSettings()
         )(mat)

       val connected = upgradeResponse.flatMap { upgrade =>
         if (upgrade.response.status == StatusCodes.SwitchingProtocols) {
           Future.successful(Done)
         } else {
           throw new RuntimeException(s"Connection failed: ${upgrade.response.status}")
         }
       }

       upgradeResponse.onComplete {
         case Success(_) => {
           logger.info("Websocket connected.")
         }
         case Failure(t) => {
           logger.error("Websocket failed.", t)
         }
       }

       closed.future.map(o => {
         logger.error("Closed.") //this is what i'm seeing when it fails
         throw new IllegalStateException("Websocket failed unexpectedly")
       })

```

---

<div class="post-metadata">

**Author:** ![pbernet](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/pbernet/32/370_2.png) [@pbernet](https://discuss.akka.io/u/pbernet)\
**Post date:** [April 7, 2022, 4:44pm UTC](https://discuss.akka.io/t/websocket-reconnect-retry-is-it-possible/9676/2 "2022-04-07T16:44:15Z")

</div>

Yes it’s possible.  
[This blog](https://blog.colinbreck.com/integrating-akka-streams-and-akka-actors-part-ii) explains, how to encapsulate akka-streams websocket clients with Actors:  
see chapter: Stream Supervision

An example implementation of this concept is here:

> <https://github.com/pbernet/akka_streams_tutorial/blob/6194bd580136f37db01e83f807b5366d502424ac/src/main/scala/sample/stream_actor/SimulateWindTurbines.scala#L19>

The section which is probably interesting for you is around here:

> <https://github.com/pbernet/akka_streams_tutorial/blob/51793b4243754726b18b9e60a8c7812c835f0054/src/main/scala/sample/stream_actor/WebSocketClient.scala#L70>

Hope that helps  
Paul

---

<div class="post-metadata">

**Author:** ![mattmar10](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/mattmar10/32/2838_2.png) [@mattmar10](https://discuss.akka.io/u/mattmar10)\
**Post date:** [April 7, 2022, 9:00pm UTC](https://discuss.akka.io/t/websocket-reconnect-retry-is-it-possible/9676/3 "2022-04-07T21:00:11Z")

</div>

Thank you, Paul! I’ll get a nice big cup of coffee and get to studying. Appreciate it immensely!
