Skip to main content

Simplifies listening to and sending notifications using PostgreSQL

Project description

py-pg-notify

PyPI - Version PyPI Downloads

py-pg-notify is a Python library that simplifies listening to and sending notifications using PostgreSQL. This package leverages asyncpg for asynchronous communication with the database, making it ideal for real-time applications.


📖 Features

  • 🔊 PostgreSQL Notifications: Easy-to-use interfaces for PostgreSQL notifications.
  • 🔄 Asynchronous: Fully asynchronous with support for multiple listeners and channels.
  • 📦 Lightweight: Built on top of asyncpg, offering high performance.
  • ⚙️ Custom Handlers: Define your notification handling logic dynamically.

🚀 Installation

Install the package via pip:

pip install py-pg-notify

Usage

Configuration Example

You can use PGConfig to manage PostgreSQL connection parameters via a dictionary, environment variables, or direct parameters.

from py_pg_notify import PGConfig

# Example using keyword args
kwargs = {
    "user": "<username>",
    "password": "<password>",
    "host": "<host>",
    "port": 5432,
    "dbname": "<dbname>"
}
config = PGConfig(**kwargs)

# Example using environment variables
# Ensure PG_USER, PG_PASSWORD, PG_HOST, PG_PORT, and PG_DBNAME are set in your environment
config = PGConfig()

# Example using direct parameters
config = PGConfig(user="<username>", password="<password>", host="<host>", port=5432, dbname="<dbname>")

Notifier Example

import asyncio
from py_pg_notify import Notifier, PGConfig

async def create_trigger_example():
    # Define the configuration using environment variables
    config = PGConfig()

    async with Notifier(config) as notifier:

        # Send a custom message to 'ch_01' channel
        await notifier.notify("ch_01", "message")

        # Set up a trigger to notify 'ch_01' when the table is modified
        await notifier.create_trigger_function("notify_function", "ch_01")
        
        await notifier.create_trigger(
            table_name="my_table",
            trigger_name="my_trigger",
            function_name="notify_function",
            event="INSERT",
        )

        trigger_functions = await notifier.get_trigger_functions("my_table")
        print("Existing Trigger Functions:", trigger_functions)

        triggers = await notifier.get_triggers("my_table")
        print("Existing Triggers:", triggers)

        await notifier.remove_trigger("my_table", "my_trigger")

        await notifier.remove_trigger_function("notify_function")

if __name__ == "__main__":
    asyncio.run(create_trigger_example())

Listener Example

import asyncio
from py_pg_notify import Listener, PGConfig, Notification

async def notification_handler(msg: Notification):
    # Perform any processing on the received notification
    print(f"Notification received: Channel={msg.channel}, Payload={msg.payload}")

async def main():
    # Define the configuration using environment variables
    config = PGConfig()

    async with Listener(config) as listener:
        await listener.add_listener("ch_01", notification_handler)
        await asyncio.sleep(3600)  # Simulate long-running process

if __name__ == "__main__":
    try: 
        asyncio.run(main())
    except KeyboardInterrupt:
        exit(0)

Examples

Refer to the examples folder for complete usage scenarios.

  1. 📬 PubSub Example - Demonstrates a simple Pub/Sub implementation using py-pg-notify.

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.


🤝 Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch.
  3. Make your changes.
  4. Add the test cases if needed.
  5. Test all the test cases for verfication.
  6. Submit a pull request.

📢 Feedback and Support

For bugs or feature requests, create an issue in the GitHub repository. We'd love to hear your feedback!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_pg_notify-1.0.2.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_pg_notify-1.0.2-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file py_pg_notify-1.0.2.tar.gz.

File metadata

  • Download URL: py_pg_notify-1.0.2.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.0

File hashes

Hashes for py_pg_notify-1.0.2.tar.gz
Algorithm Hash digest
SHA256 974852f472fcedb2d85d0dfcf9d270cd2a03e38208071a7e587dd855f18a5255
MD5 3cecea73f467c5157ead3b66f640926b
BLAKE2b-256 e5892424440eacdd9ed335d75263ae0edf38fdbcc58b314b9bb948f7f2957470

See more details on using hashes here.

File details

Details for the file py_pg_notify-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: py_pg_notify-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.11.0

File hashes

Hashes for py_pg_notify-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8012bce04125979c14a674960417a9299a26bb3444a1b469f53a11f82933d9a6
MD5 2a121ad553aa15e873ba5f5da8f3a616
BLAKE2b-256 ac9ca2304f7a8e7919e57df59ae143e4af399c085df41ecb8edf5b13469551c6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page