Build A Chat Server With Cloud Run

With Cloud Run — the fully-managed serverless container platform on Google Cloud — you can quickly and easily deploy applications using standard containers. In this article, we will explain how to build a chat server with Cloud Run using Python as the development language. We will build it with the FastAPI framework, based on this FastAPI sample source code.[Note that this article does not provide detailed descriptions of each service. Refer to other articles for details like Cloud Run settings and the cloudbuild.yaml file format.]

Chat server architecture

The chat server consists of two Cloud Run services: frontend and backend. Code management is done on GitHub. Cloud Build deploys the code, and chat messages are passed between users with Redis pub/sub and Memorystore.Set the “Authentication” option on the Cloud Run frontend service to “Allow all traffic” for frontend and backend. The two services communicate with a WebSocket, and backend and Memorystore can be connected using a serverless VPC access connector.

Let’s take a look at each service one by one.



The frontend service is written only in HTML. Only modify the WebSocket connection part with a URL of backend Cloud Run in the middle. This code is not perfect as it is just a sample to show the chat in action.

<!DOCTYPE html>
        <h2>Room: <span id="room-id"></span><br> Your ID: <span id="client-id"></span></h2>
        <label>Room: <input type="text" id="channelId" autocomplete="off" value="foo"/></label>
        <button onclick="connect(event)">Connect</button>
        <form style="position: absolute; bottom:0" action="" onsubmit="sendMessage(event)">
            <input type="text" id="messageText" autocomplete="off"/>
        <ul id='messages'>
            var ws = null;
            function connect(event) {
                var client_id =
                document.querySelector("#client-id").textContent = client_id;
                document.querySelector("#room-id").textContent = channelId.value;
                if (ws) ws.close()
                ws = new WebSocket(`wss://${channelId.value}/${client_id}`);
                ws.onmessage = function(event) {
                    var messages = document.getElementById('messages')
                    var message = document.createElement('li')
                    var content = document.createTextNode(
            function sendMessage(event) {
                var input = document.getElementById("messageText")
                input.value = ''


The Dockerfile is very simple. Because it is deployed as HTML, nginx:alpine is a good fit.

FROM nginx:alpine

COPY index.html /usr/share/nginx/html


The last part of the frontend service is the cloudbuild.yaml file. You only need to edit the project_id and “frontend”.

 # Build the container image
 - name: ''
   args: ['build', '-t', '$COMMIT_SHA', '.']
 # Push the container image to Container Registry
 - name: ''
   args: ['push', '$COMMIT_SHA']
 # Deploy container image to Cloud Run
 - name: ''
   entrypoint: gcloud
   - 'run'
   - 'deploy'
   - 'frontend'
   - '--image'
   - '$COMMIT_SHA'
   - '--region'
   - 'asia-northeast3'
   - '--port'
   - '80'

Backend Service

Let’s look at the server Python code first, starting with the core ChatServer class.

class RedisService:
    def __init__(self):
        self.redis_host = f"{os.environ.get('REDIS_HOST', 'redis://localhost')}"

    async def get_conn(self):
        return await aioredis.from_url(self.redis_host, encoding="utf-8", decode_responses=True)

class ChatServer(RedisService):
    def __init__(self, websocket, channel_id, client_id):
        super().__init__() WebSocket = websocket
        self.channel_id = channel_id
        self.client_id = client_id
        self.redis = RedisService()

    async def publish_handler(self, conn: Redis):
            while True:
                message = await
                if message:
                    now =
                    date_time = now.strftime("%Y-%m-%d %H:%M:%S")
                    chat_message = ChatMessage(
                        channel_id=self.channel_id, client_id=self.client_id, time=date_time, message=message
                    await conn.publish(self.channel_id, json.dumps(asdict(chat_message)))
        except Exception as e:

    async def subscribe_handler(self, pubsub: PubSub):
        await pubsub.subscribe(self.channel_id)
            while True:
                message = await pubsub.get_message(ignore_subscribe_messages=True)
                if message:
                    data = json.loads(message.get("data"))
                    chat_message = ChatMessage(**data)
                    await"[{chat_message.time}] {chat_message.message} ({chat_message.client_id})")
        except Exception as e:

    async def run(self):
        conn: Redis = await self.redis.get_conn()
        pubsub: PubSub = conn.pubsub()

        tasks = [self.publish_handler(conn), self.subscribe_handler(pubsub)]
        results = await asyncio.gather(*tasks)"Done task: {results}")

This is a common chat server code. Inside the ChatServer class, there is a publish_handler method and a subscribe_handler method. publish_handler serves to publish a message to the chat room (Redis) when a message comes in through the WebSocket. subscribe_handler delivers a message from the chat room (redis) to the connected WebSocket. Both are coroutine methods. Connect redis in run method and run coroutine method.This brings us to the endpoint. When a request comes in, this code connects to the WebSocket and connects to the chat server.

async def websocket_endpoint(websocket: WebSocket, channel_id: str, client_id: int):
    await manager.connect(websocket)

    chat_server = ChatServer(websocket, channel_id, client_id)

Here is the rest of the code. Combined, you get the whole code. 

import asyncio
import json
import logging
import os
from dataclasses import dataclass, asdict
from datetime import datetime
from typing import List

import aioredis
from aioredis.client import Redis, PubSub
from fastapi import FastAPI, WebSocket

logger = logging.getLogger(__name__)

app = FastAPI()

class ConnectionManager:
    def __init__(self):
        self.active_connections: List[WebSocket] = []

    async def connect(self, websocket: WebSocket):
        await websocket.accept()

    def disconnect(self, websocket: WebSocket):

    async def send_personal_message(self, message: str, websocket: WebSocket):
        await websocket.send_text(message)

    async def broadcast(self, message: dict):
        for connection in self.active_connections:
            await connection.send_json(message, mode="text")

manager = ConnectionManager()

class ChatMessage:
    channel_id: str
    client_id: int
    time: str
    message: str


The following is the Dockerfile for the backend service. Run this application with Uvicorn.

FROM python:3.8-slim
WORKDIR /usr/src/app
COPY requirements.txt  ./
RUN pip install -r requirements.txt
COPY . .
CMD [ "uvicorn", "main:app", "--host", "" ]


Put the packages for FastAPI and Redis into requirements.txt.



The last step is the cloudbuild.yaml file. Just like the frontend service, you can edit the part composed of project_id and backend, and add the IP of the memorystore created at the back into REDIS_HOST.

 # Build the container image
 - name: ''
   args: ['build', '-t', '$COMMIT_SHA', '.']
 # Push the container image to Container Registry
 - name: ''
   args: ['push', '$COMMIT_SHA']
 # Deploy container image to Cloud Run
 - name: ''
   entrypoint: gcloud
   - 'run'
   - 'deploy'
   - 'backend'
   - '--image'
   - '$COMMIT_SHA'
   - '--region'
   - 'asia-northeast3'
   - '--port'
   - '8000'
   - '--update-env-vars'
   - 'REDIS_HOST=redis://'

Cloud Build

You can set Cloud Build to automatically build and deploy from Cloud Run when the source code is pushed to GitHub. Just select “Create trigger” and enter the required values. First, select “Push to a branch” for Event.

Next, go to the Source Repository. If this is your first time, you will need GitHub authentication. Our repository also has cloudbuild.yaml, so we also select the “Location” setting as the repository. 

Serverless VPC access connector

Since both the Frontend service and the Backend service currently exist in the Internet network, you’ll need a serverless VPC access connector to connect to the memorystore in the private band. You can do this by following this example code:

gcloud compute networks vpc-access connectors create chat-connector \
--region=us-central1 \
--network=default \
--range= \
--min-instances=2 \
--max-instances=10 \

Create memorystore

To create the memorystore that will pass chat messages, use this code:

gcloud redis instances create myinstance --size=2 --region=us-central1 \

chat testTo demonstrate what you should see, we put two users into a conversation in a chat room called “test”. This will work regardless of how many users you have, and users will not see the conversations in other chat rooms until they join.


In this article, I built a serverless chat server using Cloud Run. By using Firestore instead of Memorystore, it is also possible to take the entire architecture serverless. Also, since the code is written on a container basis, it is easy to change to another environment such as GKE Autopilot, but Cloud Run is already a great platform for deploying microservices. Instances grow quickly and elastically according to the number of users connecting, so why would I need to choose another platform? Try it out now in the Cloud Console.

By: Jaeyeon Baek (Google Cloud Champion Innovator)
Source: Google Cloud Blog

For enquiries, product placements, sponsorships, and collaborations, connect with us at We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

Previous Article

SuperGaming And Google Cloud Collaborate To Empower Game Developers With A SuperPlatform

Next Article

Kubernetes Resource Usage: Estimate Workload Cost With Goldilocks Open Source

Related Posts