Search code examples
scalawebsocketscala.jshttp4s

How to create frontend project from multiple scala.js projects?


My project is a game server with frontend for it. For the frontend I'm using scala.js. Project is a cross-build so I can share files between jvm (server) and js (client). Cross-build method: https://www.scala-js.org/doc/project/cross-build.html

Client and Server is created with Http4s and for the communication I’m using Websocket.

My client is designed like in this example: https://http4s.github.io/http4s-dom/websocket.html

In my app I have fully functional game and chat for the players. The problem is that I have everything made on 1 websocket using „connectHighLevel” as you can see in above http4s example.

I wanted to make separate websocket for chat and maybe another one for game lobby. I'm struggling figuring out how to do that.

I have heard that I should make another scala.js frontend project with new websocket but then how do I add another scala.js project to my cross-build settings so it could use shared files from my current setting?

Current setting (build.sbt):

lazy val root = project.in(file("."))
  .aggregate(diceGame.jvm, diceGame.js)

Another thing is that 2 scala.js projects will generate 2 .js files and I need 1 file to use in my html file, so how do I combine them?


Solution

  • To overcome the problem, instead of creating multiple scala.js projects I have used two WebSocket connections in one project like this:

    WebSocketClient[IO].connectHighLevel(request1)
          .both(WebSocketClient[IO].connectHighLevel(request2))
          .use { case (conn1, conn2) =>