GithubHelp home page GithubHelp logo

how does rho handle websocket about rho HOT 10 CLOSED

http4s avatar http4s commented on July 22, 2024
how does rho handle websocket

from rho.

Comments (10)

shengc avatar shengc commented on July 22, 2024

coming up with an ugly hack for websocket ...

 import org.http4s.server.websocket._
 import org.http4s.websocket.WebsocketBits._

 val src = Process.constant("test").toSource.zipWithIndex.map({ case (msg, i) => Text(s"$msg - $i") })take(20)
 val snk = Process.halt

 // unfortunately websocketkey and websocket case class are both defined in a protected scope that as a user cannot access
 val attrs = WS(Exchange(src, snk)).run.attributes.entries.toList
 attrs.foldLeft(NotImplemented("this is a websocket route"))(_.withAttribute(_))

Besides exposing too many internal logic, this solution does not allow response evaluated in a separate thread pool. In normal HttpService, I can use Task.fork to easily run the response in a different thread pool, but with rho dsl, there seems to be no easy to achieve the same effect.

from rho.

bryce-anderson avatar bryce-anderson commented on July 22, 2024

I have given no consideration to websockets in rho as of yet.

The problem shouldn't be the Task but the Response: who deals in Results to keep some compile time information around for use in swagger generation etc, which is just a wrapper around Response with some extra type information.

from rho.

bryce-anderson avatar bryce-anderson commented on July 22, 2024

@shengc, I have a PR to address this, but I'm not sure when it will hit a release. In the intrum, you could copy the ResultMatcher code and bring the implicit into scope manually.

from rho.

shengc avatar shengc commented on July 22, 2024

@bryce-anderson,

I think I got what you mean. I have rewritten the solution I came up with, which now seems more reasonable ...

Task.fork {
   import org.http4s.server.websocket._
   import org.http4s.websocket.WebsocketBits._

   val src = Process.constant("test").toSource.zipWithIndex.map({ case (msg, i) => Text(s"$msg - $i") })take(20)
   val snk = Process.halt

   WS(Exchange(src, snk)).flatMap(_.attributes.entries.foldLeft(NotImplemented("this is a websocket route"))(_.withAttribute(_)))
}

from rho.

shengc avatar shengc commented on July 22, 2024

@bryce-anderson, yes that result matcher definitely is helpful :-)

from rho.

shengc avatar shengc commented on July 22, 2024

This raises another problem though. Look at this example,

GET / "number" +? param[Int]("num") |>> { (num: Int) =>
  if (num < 0) BadRequest("num <= 0 :-(")
  else WS(Exchange(Process.constant("test").take(num).map(Text(_)), Process.halt))
}

The implicits get messed up in this case, since BadRequest does not get resolved to a Task[Response]. The only workaround I have for this is to explicitly use org.http4s.dsl.BadRequest(...) so that an implicit ResultMatcher[Response] can be resolved. It is definitely an unpleasant hack, wondering if there is a more natural way to address this ?

from rho.

bryce-anderson avatar bryce-anderson commented on July 22, 2024

There is no easy way to make the types unify there. What you've done is the best we can do with the current state of affairs. To make it better, rho will need to get a first class notion of WS.

from rho.

bryce-anderson avatar bryce-anderson commented on July 22, 2024

I do remember another way you can make this work, but you lose any swagger information (if you were using it at all): try BadRequest.pure("num <= 0 :-("). that will make a Task[Response] instead of a Task[Result] and will match the WS result type.

from rho.

shengc avatar shengc commented on July 22, 2024

Thanks :) Does swagger support websocket anyway ?

from rho.

bryce-anderson avatar bryce-anderson commented on July 22, 2024

Nope.

On Wed, Jul 15, 2015 at 8:57 PM, Mianwo [email protected] wrote:

Thanks :) Does swagger support websocket anyway ?


Reply to this email directly or view it on GitHub
#79 (comment).

from rho.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.