Jhoan

Modern Backend Architecture: Bridging PHP and Python in Real-Time using NATS

this is a demonstration of how to implement nats.io with Laravel and Python.

repository: https://github.com/jhovadev/php-python-nats-connection-example

tl:dr

In a traditional monolithic PHP app, a heavy task (like generating a report or processing an image) blocks the user's request or requires complex cron jobs. By introducing Python workers listening on NATS, we move to an Event-Driven Architecture. The PHP app simply fires an event (tasks.created) and returns an immediate success message to the user. Somewhere else in the cluster, a Python worker wakes up, processes the data, runs an AI model, and acknowledges the job. This makes the user interface feel instant, regardless of how complex the backend processing is.

tools that i used ( u can use yours )

libraries:

architecture

PlantUML diagram

docker-compose setup:

services:
  nats_server:
    image: nats:latest
    container_name: nats-server
    ports:
      - "4222:4222"   # Client connections
      - "8222:8222"   # HTTP monitoring
      - "6222:6222"   # Cluster routing (optional)
    command: ["--jetstream", "--http_port", "8222", "-m", "8222"]
    volumes:
      - nats_data:/data
    restart: unless-stopped

volumes:
  nats_data:

references

built with btw btw logo