Skip to content

📋 Google Cloud Functions used to send data using Python code to Google Cloud Pub/Sub where data can be published with a message

Notifications You must be signed in to change notification settings

AndrewJBateman/python-functions-pubsub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚡ Python Functions Pub/Sub

  • Google Cloud Functions used to send data using Python code to Google Cloud Pub/Sub where data can be published with a message
  • Code from D-I-Ry with modifications - see 👏 Inspiration below
  • Note: to open web links in a new window use: ctrl+click on link

GitHub repo size GitHub pull requests GitHub Repo stars GitHub last commit

📚 General info

  • Pub/Sub used to pass messages between services and to trigger Cloud Functions to run workflow
  • To subscribe to data in Cloud Pub/Sub, you create a subscription based on the topic, and subscribe to that, passing a callback function.
  • Python code deployed from VS Code terminal
  • Cloud Build API has to be enabled for deployment to work. Mine was already enabled from a similar previous project
  • From Pub/Sub factor-sensors topic is triggered by message published to pubsub factory sensors topic

📷 Screenshots

screen print screen print screen print

📶 Technologies

💾 Setup

  • Ref. tutorial
  • When creating a Pub/Sub I found that the subscription was created automatically
  • gcloud functions deploy automation_readings --runtime python39 --trigger-http --allow-unauthenticated to deploy to Cloud Functions. Python code can be viewed in Cloud Functions automation_readings SOURCE menu. Message logs can be viewed under the LOGS menu. Logs can also be viewed in Firebase Console Logs menu.
  • python sendMessage.py to run sendMessage.py. This will trigger the Cloud Function using a http trigger URL and will supply sensor readings payload data in JSON format to the Cloud Functions Topic automation_readings. A response is returned with a status code. Clicking on 'PULL' in Cloud Pubsub Topic Messages will display message

💻 Code Examples

  • main.py extract - to move data to Pubsub. Topic path copied from Cloud Pub/Sub Subscription Details/Topic name
    topic_path = 'projects/python-pubsub-firestore/topics/factory-automation'

    message_json = json.dumps({
        'data': {'message': 'automation sensor readings'},
        'readings': {
            'sensorName': sensor_name,
            'sensorReference': sensor_reference,
            'temperature': temperature,
            'humidity': humidity
        }
    })
    message_bytes = message_json.encode('utf-8')

    try:
        publish_future = publisher.publish(topic_path, data=message_bytes)
        publish_future.result() # verify that the publish succeeded
    except Exception as e:
        print(e)
        return (e, 500)

    return ('Message received and published to Pubsub', 200)

🆒 Features

  • Cloud Pub/Sub decoupling senders and receivers, allowing secure and highly available communications between independently written applications.

📋 Status & To-do list

  • Status: Working
  • To-do: Nothing

👏 Inspiration

✉️ Contact

About

📋 Google Cloud Functions used to send data using Python code to Google Cloud Pub/Sub where data can be published with a message

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages