Upgrade to WebSocket required on Play Scala app

162 views
Skip to first unread message

Isuru Samaraweera

unread,
Apr 24, 2017, 4:39:29 PM4/24/17
to Play Framework
Hi,
I am trying to run a playframework 2.5 /scala twitter application based on web sockets on chrome browser.
When I run the app I am getting the text message in browser as 'Upgrade to WebSocket required'

Intended behavior is to display tweet in the browser.

Some sample codes are as below 
controller

def index = Action { implicit request =>
Ok(views.html.index("Tweets"))
}

index.scala.html

<div id="tweets"></div>

    <script type="text/javascript">

        function appendTweet(text) {
            var tweet = document.createElement("p");
            var message = document.createTextNode(text);
            tweet.appendChild(message);
            document.getElementById("tweets").appendChild(tweet);
        }

        function connect(attempt) {
            var connectionAttempt = attempt;
            var tweetSocket = new WebSocket("@routes.HomeController.tweets().webSocketURL()");
            tweetSocket.onmessage = function (event) {
                console.log(event);
                var data = JSON.parse(event.data);
                appendTweet(data.text);
            };
            tweetSocket.onopen = function() {
                connectionAttempt = 1;
                tweetSocket.send("subscribe");
            };
            tweetSocket.onclose = function() {
                if (connectionAttempt <= 3) {
                    appendTweet("WARNING: Connection with the server lost, attempting to reconnect. Attempt number " + connectionAttempt);
                    setTimeout(function() {
                        connect(connectionAttempt + 1);
                    }, 5000);
                } else {
                    alert("The connection with the server was lost. Please try again later. Sorry about that.");
                }
            };
        }

        connect(1);
    </script>

Please tell whats happening why its not working as expected.

Thanks,
Isuru

Igmar Palsenberg

unread,
Apr 24, 2017, 4:51:14 PM4/24/17
to Play Framework


Op maandag 24 april 2017 22:39:29 UTC+2 schreef Isuru Samaraweera:
Hi,
I am trying to run a playframework 2.5 /scala twitter application based on web sockets on chrome browser.
When I run the app I am getting the text message in browser as 'Upgrade to WebSocket required'

Intended behavior is to display tweet in the browser.

Some sample codes are as below 
controller

def index = Action { implicit request =>
Ok(views.html.index("Tweets"))
}

<snip>
 
Please tell whats happening why its not working as expected.

Your javascript code tries to open a websocket connection, but the controller above doesn't provide it. There is no automatic upgrade of a connection, and your controller must return a Websocket type. As a sidenote : All the usual things about the Result return type don't apply. 



Igmar

Isuru Samaraweera

unread,
Apr 24, 2017, 11:34:39 PM4/24/17
to Play Framework
Hi Igmar,

Here is my controller for tweets action as invoked in the java script.

   def tweets = WebSocket.accept[String, JsValue] { implicit request =>
    ActorFlow.actorRef(out => TwitterStreamer.props(out))
  }

And the twitterstreamer is like this


import akka.actor.{ Actor, ActorRef, Props }
import play.api.Logger
import play.api.libs.json.Json
class TwitterStreamer(out: ActorRef) extends Actor {
  def receive = {
    case "subscribe" =>
      Logger.info("Received subscription from a client")
      out ! Json.obj("text" -> "Hello, world!")
  }
}
object TwitterStreamer {
  def props(out: ActorRef) = Props(new TwitterStreamer(out))
}


Can you suggest what has gone wrong here?

Thanks
Isuru

Isuru Samaraweera

unread,
Apr 26, 2017, 4:13:19 AM4/26/17
to Play Framework
Still I am getting Upgrade to WebSocket required message o nthe browse when I call tweets action.
What can be the cause?


On Tuesday, April 25, 2017 at 2:09:29 AM UTC+5:30, Isuru Samaraweera wrote:

Igmar Palsenberg

unread,
Apr 26, 2017, 5:49:30 AM4/26/17
to Play Framework


Op woensdag 26 april 2017 10:13:19 UTC+2 schreef Isuru Samaraweera:
Still I am getting Upgrade to WebSocket required message o nthe browse when I call tweets action.
What can be the cause?

Is there a proxy / loadbalancer if front of that Play app ?


Igmar

Isuru Samaraweera

unread,
Apr 26, 2017, 6:00:15 AM4/26/17
to Play Framework
Hi,
I am running netty locally from activator.And browser also in the same machine.So no load balancer or proxy involved.

Thanks,
Isuru


On Tuesday, April 25, 2017 at 2:09:29 AM UTC+5:30, Isuru Samaraweera wrote:

Isuru Samaraweera

unread,
Apr 27, 2017, 12:58:45 AM4/27/17
to Play Framework
Hi Igmar,
Do you have any clue on this?

Thanks


On Tuesday, April 25, 2017 at 2:09:29 AM UTC+5:30, Isuru Samaraweera wrote:

Isuru Samaraweera

unread,
May 2, 2017, 10:27:38 AM5/2/17
to Play Framework
Hi Igmar,
Now web socket is fine..How ever corresponding actor is not invoked.
Here is the controller action.

   def tweets = WebSocket.accept[String, JsValue] { implicit request =>
     println("here")
    ActorFlow.actorRef(out => TwitterStreamer.props(out))
  }

'here; is printed and actor is not invoked.Here is the actor.

class TwitterStreamer(out: ActorRef) extends Actor {
   def receive = {
    case msg: String =>
      out ! ("I received your message: " + msg)
  }
    
}
object TwitterStreamer {
  def props(out: ActorRef) = Props(new TwitterStreamer(out))
}

Can you see why its not going in to the actor?

Thanks
Isuru


On Tuesday, April 25, 2017 at 2:09:29 AM UTC+5:30, Isuru Samaraweera wrote:
Reply all
Reply to author
Forward
0 new messages