Presence
is a stateful service which has no UI. It uses a Vote CRDT
to track the state (online/offline status) of the user entity:
-
entity key =
User
defined by the user name -
value =
OnlineStatus
which is a boolean value
The diagram below shows the chat application is architected as 3 microservices, deployed in a Kubernetes cluster.
For now, let’s ignore the application flow which is explained in Cloudstate Chat Sample. Also, we’ll skip the coding tutorial which is very similar to that of the Friends
service: JavaScript implementation of the Friends service. In this section, we focus only on the build and the testing of the Presence
service:
Build a docker image with the following name attributes:
-
Docker registry ID. Let’s assume your DockerID on https://hub.docker.com/ is
mydockerregistry
-
image name:
chat-presence-js
-
version:
latest
The docker image tag will be mydockerregistry/chat-presence-js:latest
cd ./presence
DOCKER_PUBLISH_TO=mydockerregistry
# build docker image
docker build . -t $DOCKER_PUBLISH_TO/chat-presence-js:latest
# authenticate with your Docker registry
docker login
# push the docker image to your registry
docker push $DOCKER_PUBLISH_TO/chat-presence-js:latest
Testing the Presence
service follows the same procedure as that of the Friends
service. You may want to read the Testing Friends service section for a more detailed introduction.
-
Edit docker-compose.yaml to replace the prebuilt image
chat-presence-js
by the docker image you have just published in the previous step. -
Start the docker images of both the Cloudstate proxy and the
Presence
service
$ cd (where docker-compose.yaml is located)
$ docker-compose up
We can use gRPCurl
to test our service. Make sure you have installed gRPCurl
as mentioned in the Tools Prerequisites section.
Discover the services exposed at our gRPC server, implemented by the Cloudstate proxy:
$ grpcurl -plaintext localhost:9002 list
# console output
cloudstate.samples.chat.presence.Presence
grpc.reflection.v1alpha.ServerReflection
Then review the metadata of the Presence
service
$ grpcurl -plaintext localhost:9002 describe cloudstate.samples.chat.presence.Presence
# console output
cloudstate.samples.chat.presence.Presence is a service:
service Presence {
rpc Connect ( .cloudstate.samples.chat.presence.User ) returns ( stream .cloudstate.samples.chat.presence.Empty );
rpc Monitor ( .cloudstate.samples.chat.presence.User ) returns ( stream .cloudstate.samples.chat.presence.OnlineStatus );
}
Finally, let’s test the Connect
and Monitor
methods. These methods require input parameters which should be serialized as JSON string. The data schema is defined in Presence service descriptor.
The Connect
and Monitor
methods return to the client a response as a stream. The client here is the terminal session running grpcurl
. The client must be listening continuously to receive the response stream (ie. the terminal session looks like it is stuck in an infinite loop).
# Open a new terminal session and run (Ctrl-C to stop/restart)
$ grpcurl -plaintext -d '{"name": "Albert"}' \
localhost:9002 \
cloudstate.samples.chat.presence.Presence/Monitor
# console output: {} means user "Albert" is not yet connected
{
}
# Open a new terminal session and run (Ctrl-C to stop/restart)
$ grpcurl -plaintext -d '{"name": "Albert"}' \
localhost:9002 \
cloudstate.samples.chat.presence.Presence/Connect
# console output:
# - nothing in this terminal
# - however, in the terminal where the `Monitor` method is running, the console should output
{
"online": true
}
ℹ️
|
You can also monitor the request/response activities of the gRPC server by checking the logs in the terminal session running docker-compose
|
The deployment is described in details in the Cloudstate Chat Sample. This is a chat room application with a basic UI allowing to test the integration of multiple Cloudstate services collaborating together.