ExpressWS API for Scala.js
express-ws - WebSocket endpoints for Express applications.
Description
WebSocket endpoints for Express applications. Lets you define WebSocket endpoints like any other type of route, and applies regular Express midddleware like for anything else.
Version 2.0 of this library contains a breaking change. Please make sure to read CHANGELOG.md before upgrading.
Build Dependencies
Build/publish the SDK locally
$ sbt clean publish-local
Running the tests
Before running the tests the first time, you must ensure the npm packages are installed:
$ npm install
Then you can run the tests:
$ sbt test
Examples
import io.scalajs.npm.express._
import io.scalajs.npm.expressws._
import io.scalajs.util.JSONHelper._
import io.scalajs.util.ScalaJsHelper._
import scala.scalajs.js
val app = Express().withWsRouting
val expressWs = ExpressWS(app)
app.use((req: Request, res: Response, next: js.Function0[Unit]) => {
println("middleware")
req.dynamic.testing = "testing"
next()
})
app.get("/", (ws: WS, req: Request, res: Response, next: js.Function0[Unit]) => {
println(s"get route ${req.dynamic.testing}")
res.end()
})
app.ws("/", (ws: WS, req: Request) => {
ws.onMessage { msg =>
println(msg.toJson)
}
println(s"socket: ${req.dynamic.testing}")
})
app.listen(3000)
Artifacts and Resolvers
To add the ExpressWS
binding to your project, add the following to your build.sbt:
libraryDependencies += "io.scalajs.npm" %%% "express-ws" % "0.5.0"
Optionally, you may add the Sonatype Repository resolver:
resolvers += Resolver.sonatypeRepo("releases")