Building application logic
A chat application is all about exchanging messages between users, so we need to define a message type. We can find its definition in the first line of model.opa
:
type message = {string user, string text}
It's a very simple type with two fields. The user
field represents the author of the message, and the text
field represents the content of the message.
Now that we have the definition of a message, we need a way to pass the messages between different clients. As mentioned in Chapter 5, Communicating between Client and Server, Opa provides three ways for communicating between clients and servers: session, cell, and network. Session is for one-way asynchronous communication; cell is a special case of session and is for two-way synchronous communication; and network is for broadcasting messages to all observers. Network is the right choice for our purpose:
server private Network.network(message) room = Network.empty();
This code fragment defines an empty network named...