Skip to content

Latest commit

 

History

History
54 lines (37 loc) · 2.19 KB

README.md

File metadata and controls

54 lines (37 loc) · 2.19 KB

Example project to test Websockets with Google Cloud Pub/Sub

This is a PoC of how to use Python 3+, AIOHTTP and Google Cloud Pub/Sub to forward messages from a subscription to a web client by using websockets.

Prerequisites

First of all, obviously you should have a Google Cloud account and a configured project with enabled Pub/Sub in order to make some tests. The following links will help you to create the topic, subtopic and get the credentials in json format:

In addition, you should export the following environment variables:

$ export GOOGLE_CLOUD_PROJECT=your_gcloud_project
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials.json
$ export GOOGLE_CLOUD_PUBSUB_TOPIC=your_topic_name
$ export GOOGLE_CLOUD_PUBSUB_SUBSCRIPTION=your_subscription_name

Installing

In order to set up the environment you should have installed Python 3.7 and create a virtual env. To do that, run the following commands in your terminal:

$ mkdir venv
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt

Running the server

To run the server just execute the following command (remember export the previous env variables):

$ python app.py venv

Once the server is running you can use your browser pointing at http://localhost:8080/index.html to see a very simple web page showing the last message in the topic and the incoming messages in real time.

Send a message to the topic

To see how a new message goes to the Pub/Sub and the webpage changes by using websockets, you can use the next helper command:

$ python pub.py my-new-message

Built With