PHP Classes

File: examples/case-studies/system/communication/threaded_notification_dispatcher.md

Recommend this page to a friend!
  Packages of Christos Drogidis   Ascoos OS   examples/case-studies/system/communication/threaded_notification_dispatcher.md   Download  
File: examples/case-studies/system/communication/threaded_notification_dispatcher.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: Ascoos OS
A PHP Web 5.0 Kernel for decentralized web and IoT
Author: By
Last change: Update of examples/case-studies/system/communication/threaded_notification_dispatcher.md
Date: 9 months ago
Size: 2,635 bytes
 

Contents

Class file image Download

Threaded Notification Dispatcher

This case study demonstrates how Ascoos OS can be used to send notifications to multiple recipients concurrently using threads and the Telegram API. It leverages asynchronous execution, observer-based event tracking, and dynamic notification dispatch.

Purpose

This example uses the following Ascoos OS classes: - TThreadHandler: Executes multiple tasks concurrently using threads. - TTelegramAPIHandler: Sends messages via the Telegram Bot API. - TObserverHandler: Tracks and logs notification events using observers.

Structure

The case study is implemented in a single PHP file: - threaded_notification_dispatcher.php: Includes threaded dispatch logic, Telegram integration, and observer notification.

Prerequisites

  1. Install Ascoos OS (main repository).
  2. Set up a Telegram bot and obtain the `bot_token`.
  3. Ensure write permissions for `$AOS_LOGS_PATH`.
  4. The phpBCL8 library is preinstalled and auto-loaded.

Getting Started

  1. Replace `your_bot_token` and `your_chat_id` with actual values.
  2. Run the script via a web server:
    https://localhost/aos/examples/case-studies/system/communication/threaded_notification_dispatcher.php
    

Example Usage

$observerHandler = TObserverHandler::getInstance([$properties['logs']]);
$observerHandler->attach(new SystemObserver(), 10);

$threadHandler->startThread('task_chat_id'.$chat_id, function () use (...) {
    $telegram = new TTelegramAPIHandler(...);
    $telegram->sendMessage(['chat_id' => $chat_id, 'text' => $message]);
    $observerHandler->notify(['event' => 'telegram.sent', 'data' => ['chat_id' => $chat_id]]);
});

Expected Output

The script sends a message to each recipient and logs success or failure. Example log entries:

System event observed: {"event":"telegram.sent","data":{"chat_id":"123456789"}}
System event observed: {"event":"telegram.failed","data":{"chat_id":"987654321"}}

Resources

Contributing

Want to contribute to this case study? Fork the repository, modify or extend threaded_notification_dispatcher.php, and submit a pull request. See CONTRIBUTING.md for guidelines.

License

This case study is licensed under the Ascoos General License (AGL). See LICENSE.