FastAPI Event Emitter¶
      The FastAPIEventEmitter provides a powerful way to build reactive FastAPI
    applications using an event-driven architecture. It leverages FastAPI's asynchronous BackgroundTasks
    to handle events outside the request-response cycle.
What is it?¶
      The FastAPIEventEmitter is a concrete implementation of the EventEmitter class that utilizes 
    FastAPI's BackgroundTasks for event handling. It provides a convenient way to incorporate event-driven 
    functionality into FastAPI applications, allowing you to implement tasks such as sending emails in a 
    decoupled and asynchronous manner.
How it Works¶
      The FastAPIEventEmitter handles the emission and processing of events by utilizing the FastAPI's 
    background tasks queue. When an event is emitted, its execution is scheduled into the FastAPI's background
    tasks to run asynchronously after the response is sent.
Usage¶
    To start using the FastAPIEventEmitter, follow these steps:
- 
Install Dependencies: Ensure FastAPI and Pyventus are installed. 
- 
Dependency injection and usage: The FastAPIEventEmitterintegrates fully withFastAPIand can be used in routes or elsewhere via dependency injection. As an example, we'll create a simpleFastAPIapp to simulate a non-blocking email notification. Create amain.pyfile and add the following code:
- 
Run the server: Start the app with: Open your browser at http://127.0.0.1:8000/. You will see the JSON response as: You'll also be able to see the outputs of the functions in the console logs as follows: INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [28720] INFO: Started server process [28722] INFO: Waiting for application startup. INFO: Application startup complete. INFO: 127.0.0.1:57926 - "GET / HTTP/1.1" 200 OK Sending email to: email@pyventus.com Email sent successfully!
Recap¶
      As we have seen, the FastAPIEventEmitter allows building reactive FastAPI apps using an event-driven 
    architecture. By leveraging background tasks, events can be emitted from routes and processed independently without
    blocking responses. This delivers asynchronous and non-blocking behavior for tasks like emails, jobs, streams and
    more. The emitter integrates seamlessly with FastAPI via dependency injection.