http4s icon indicating copy to clipboard operation
http4s copied to clipboard

Ember doesn't process websocket messages on time

Open grzegorz-bielski opened this issue 2 years ago • 2 comments

Discussed on Discord: https://discord.com/channels/632277896739946517/632286375311573032/1000496748004921506

When more than 1 message is send to the http4s backed by Ember at once, the subsequent messages seems to be stuck in some internal queue.

Given an echo server like this:

object WSExample extends IOApp.Simple with Http4sDsl[IO]:
  def run =
    for
      _ <- EmberServerBuilder
        .default[IO]
        .withHostOption(Host.fromString("0.0.0.0"))
        .withPort(Port.fromInt(3000).get)
        .withHttpWebSocketApp { builder =>
          HttpRoutes.of[IO] { case GET -> Root / "endpoint" =>
              val inOut: Pipe[IO, WebSocketFrame, WebSocketFrame] =
                _.map { m =>
                  println(m.toString)
                  m
                }
              builder.build(inOut)
            }
            .orNotFound
        }
        .withErrorHandler { e =>
          IO.println("Could not handle a request" -> e) *> InternalServerError()
        }
        .build
        .use(_ => IO.never)
        .void
    yield ()

and index.html with a script like this, that runs in Chrome:

const socket = new WebSocket("ws://localhost:3000/endpoint");
socket.addEventListener('open', event => {
    socket.send("1")
    socket.send("2")
    socket.send("3")
})

socket.addEventListener('message', event => {
  console.log('Message from server ', event.data)
})

Even though all messages: "1", "2" and "3" are correctly send to the server, only the "1" reaches the inOut pipe. But after executing socket.send("4") in the JS console, the "2" suddenly appears in the inOut's println.

Tested on 0.23.13 http4s-ember-server. Everything seems to be working fine on 0.23.12 http4s-blaze-server.

grzegorz-bielski avatar Jul 23 '22 21:07 grzegorz-bielski

I had similar problems in https://github.com/http4s/http4s-dom/pull/93#issuecomment-1025237877 (although only in CI, I was unable to reproduce locally) and I also heard a similar report from @buntec.

armanbilge avatar Jul 23 '22 21:07 armanbilge

Also linking to https://github.com/http4s/http4s/issues/6420#issuecomment-1138864091, possibly related?

armanbilge avatar Jul 25 '22 16:07 armanbilge