Link Search Menu Expand Document

Websockets

fritz2 offers support for websockets you can use with different protocols. To use it, you first have to create a socket:

val websocket: Socket = websocket("ws://myserver:3333")

You can specify one or more protocols on creation. See these docs for more information.

Your socket is now ready to establish a Session with the server, using the method connect(). Messages can now be exchanged between socket and server, which looks like this:

val session: Session = websocket.connect()

// receiving messages from server
session.messages.body.onEach {
  window.alert("Server said: $it")
}.watch() // watch is needed because the message is not bound to html

// sending messages to server
session.send("Hello")

As you can see, Session offers a Flow of MessageEvents in messages. When a new message from the server arrives, a new message pops up on the Flow. Get the content of the message with one of the following methods (depending on content type):

  • data: Flow<Any?>
  • body: Flow<String>
  • blob: Flow<Blob>
  • arrayBuffer: Flow<ArrayBuffer>

More information regarding the connection status is provided by the Session as Flows:

  • isConnecting: Flow<Boolean>
  • isOpen: Flow<Boolean>
  • isClosed: Flow<Boolean>
  • opens: Flow<Event>
  • closes: Flow<CloseEvent>

When you’re done, close the session client-side. Supplying a code and a reason is optional.

session.close(reason = "finished")

After the Session was closed by either client or server, no further messages can be sent, and trying to do so throws a SendException.

You can synchronize the content of a Store with a server via websockets. Use the function syncWith(socket: Socket, resource: Resource<T, I>) like in the following example:

data class Person(val name: String, val age: Int, val _id: String = uniqueId())

val personResource = Resource(Person::_id, PersonSerializer, Person("", 0))
val socket = websocket("ws://myserver:3333")

val entityStore = object : RootStore<Person>(personResource.emptyEntity) {
    init {
        syncWith(socket, personResource)
    }
    // your handlers...
}

When the model in the Store changes, it will be sent to the server via websocket, and vice versa of course.

That way fritz2 provides an easy way to synchronize your stores with a server. Want more? Keep on reading about Routing.


Distributed by a MIT license.