Building a Chat app with Docker and gRPC
What Will I Learn?
- You will learn how to add a Streaming Service to a gRPC
- You will learn how to put a Go Service in a Docker Container
- You will learn how to model a Chat application
- You will learn about WaitGroups and Go Routines
- You will learn how to block returns with channels
- FreeBSD 10.3 or later
- Linux 2.6.23 or later with glibc
- macOS 10.10 or later
- Windows 7, Server 2008R2 or later
- An understanding of APIs
- A basic understanding of Go
- Some understanding of micro service architecture.
Resources for Go and this Project:
- Awesome Go Github: https://github.com/avelino/awesome-go
- Golang Installation Page: https://golang.org/dl/
- Golang Home Page: https://golang.org/
- Golang Documentation Page: https://golang.org/doc/
- gRPC Website: https://grpc.io/
- Protocol Buffers Website: https://developers.google.com/protocol-buffers/
- Docker Website: https://www.docker.com/
- Go Logo: https://golang.org/
In this Go video tutorial, we take a look at a more advanced gRPC example in the form of a simple Console Chat application. This gRPC Chat Application makes use of a stream to push messages from the server to the clients. We also take the server and put it into a Docker Container to make it easy for deployment and for containerization. The application makes use of many goroutines and a
WaitGroup to synchronize these goroutines.
Streaming from the Server to the Client with gRPC
Like any RPC system, gRPC is primarily focused around allowing methods to be called remotely. To facilitate this functionality, there are four primary types of RPCs that can be specified with gRPC. In the prior tutorial, we took a look at unary RPC services exclusively. These are the most basic forms of RPC services that can be defined with gRPC. In this tutorial however, we add on a server streaming RPC service in conjunction with a unary RPC service.
In this image, are the two main services for our Chat Application. The
CreateStream service is the server streaming service, denoted by the
stream keyword in the returns statement. When a client connects to the server, the server will open a stream to the client. This will then allow the server to stream any received messages to the client and any other active clients which are also connected.
Using Docker Containers for Go and gRPC Microservices
Docker is a revolutionary tool, that was also build with Go, that makes it easier to create, deploy and run applications inside of containers. A container is a small environment detached from the major Operating System which contains all of the tools needed to build and execute the enclosed application. In this way, docker is a bit like a virtual machine; however, unlike a virtual machine, docker doesn't create an entire virtual operating system.
For the example application, we make use of Docker to serve the server module of the Chat Application. Using Docker in this manner, allows us to not only create a container to compile the server but also a separate container to run the application. In the image above, we derive the container from an Alphine Linux kernel that contains the golang toolchain. We also install a bunch of tools that are necessary for compiling the application using the
apk package manager. Afterwards, the folder structure of the original project is rebuilt inside of the container to make it easier to execute the application.
The Source Code for this video may be found here: https://github.com/tensor-programming/docker_grpc_chat_tutorial