English Version
This guide explains the practical steps for sending messages through the various MMS channels. It covers the crucial difference between queued (asynchronous) and direct (synchronous) sending, and provides detailed code examples for each channel.Queued vs. Direct Sending: A Key Architectural Concept
It’s vital to understand that the MMSsend() method itself is always synchronous—it executes immediately when called. The asynchronous (queued) behavior comes from how you initiate the process.
1. Queued Sending (Asynchronous) - The Standard & Recommended Approach
This is the best practice for performance and user experience, especially in a web context (e.g., after a user registers or places an order). You dispatch a Job to the queue, and your application can immediately return a response to the user without waiting for the email or message to be sent. A separate queue worker process handles the actual sending in the background. When to use: In Controllers, Listeners, or any part of your code that handles user-facing requests. The Code Pattern: Use the staticdispatch() method on your Job class.
HTTP Request -> dispatch(Job) -> (Instant Response to User) … [Queue Worker in Background] … Job->handle() -> Mms::channel()->send()
2. Direct Sending (Synchronous) - For Specific Use Cases
This pattern executes the entire sending process immediately and blocks until it is complete. It’s useful in contexts where you need the action to finish before proceeding, such as in an Artisan command or during testing. When to use: In Artisan commands, tests, or specific background tasks that require immediate execution. The Code Pattern: Create a new instance of the Job and call itshandle() method directly.
Artisan Command -> new Job() -> $job->handle() -> Mms::channel()->send() -> (Command waits for completion)
The test:mms --direct command uses this exact pattern.
Channel-Specific Sending Guides
The following examples show the code inside thehandle() method of each Job. Remember, you can call this method directly for synchronous sending or let the queue worker call it by using Job::dispatch().
1. Sending a WhatsApp Message
- Key Data:
recipient(phone number),body. - Example (
app/Jobs/SendWhatsAppJob.php):
2. Sending an Email
- Key Data:
recipient(email address),body(HTML),subject,name. - Example (
app/Jobs/SendEmailJob.php):
3. Sending an FCM Push Notification
- Key Data:
recipient(FCM token),body,title,image(optional). - Example (
app/Jobs/SendFcmJob.php):
4. Sending a Telegram Message
- Key Data:
recipient(Chat ID),body. - Example (
app/Jobs/SendTelegramJob.php):
5. Sending an Internal (Database) Notification
- Key Data:
recipient(User ID),body,title, and custom structured data (e.g.,entity). - Example (
app/Jobs/SendInternalNotification.php):
Versi Bahasa Indonesia
Panduan ini menjelaskan langkah-langkah praktis untuk mengirim pesan melalui berbagai channel MMS. Panduan ini mencakup perbedaan krusial antara pengiriman melalui antrean (asinkron) dan langsung (sinkron), serta menyediakan contoh kode terperinci untuk setiap channel.Pengiriman Via Antrean vs. Langsung: Sebuah Konsep Arsitektur Kunci
Sangat penting untuk memahami bahwa methodsend() milik MMS itu sendiri selalu sinkron—ia dieksekusi secara langsung saat dipanggil. Perilaku asinkron (menggunakan antrean/queue) berasal dari cara Anda memulai proses tersebut.
1. Pengiriman Via Antrean (Asinkron) - Pendekatan Standar & Direkomendasikan
Ini adalah praktik terbaik untuk performa dan pengalaman pengguna, terutama dalam konteks web (misalnya, setelah pengguna mendaftar atau melakukan pesanan). Anda mengirimkan (dispatch) sebuah Job ke dalam antrean, dan aplikasi Anda dapat langsung mengembalikan respons kepada pengguna tanpa menunggu email atau pesan terkirim. Proses queue worker yang terpisah menangani pengiriman sebenarnya di latar belakang. Kapan digunakan: Di dalam Controller, Listener, atau bagian mana pun dari kode Anda yang menangani permintaan dari pengguna. Pola Kode: Gunakan method statisdispatch() pada kelas Job Anda.
Request HTTP -> dispatch(Job) -> (Respons Instan ke Pengguna) … [Queue Worker di Latar Belakang] … Job->handle() -> Mms::channel()->send()
2. Pengiriman Langsung (Sinkron) - Untuk Kasus Tertentu
Pola ini mengeksekusi seluruh proses pengiriman secara langsung dan akan berhenti (block) sampai selesai. Ini berguna dalam konteks di mana Anda memerlukan tindakan tersebut selesai sebelum melanjutkan, seperti dalam command Artisan atau saat pengujian. Kapan digunakan: Di dalam command Artisan, tes, atau tugas latar belakang spesifik yang memerlukan eksekusi segera. Pola Kode: Buat instance baru dari Job dan panggil methodhandle()-nya secara langsung.
Command Artisan -> new Job() -> $job->handle() -> Mms::channel()->send() -> (Command menunggu hingga selesai)
Command test:mms --direct menggunakan pola ini.
Panduan Pengiriman Spesifik per Channel
Contoh-contoh berikut menunjukkan kode di dalam methodhandle() dari setiap Job. Ingat, Anda dapat memanggil method ini secara langsung untuk pengiriman sinkron atau membiarkan queue worker memanggilnya dengan menggunakan Job::dispatch().
1. Mengirim Pesan WhatsApp
- Data Kunci:
recipient(nomor telepon),body. - Contoh (
app/Jobs/SendWhatsAppJob.php):
2. Mengirim Email
- Data Kunci:
recipient(alamat email),body(HTML),subject,name. - Contoh (
app/Jobs/SendEmailJob.php):
3. Mengirim Notifikasi Push FCM
- Data Kunci:
recipient(token FCM),body,title,image(opsional). - Contoh (
app/Jobs/SendFcmJob.php):
4. Mengirim Pesan Telegram
- Data Kunci:
recipient(Chat ID),body. - Contoh (
app/Jobs/SendTelegramJob.php):
5. Mengirim Notifikasi Internal (Database)
- Data Kunci:
recipient(User ID),body,title, dan data terstruktur kustom (misalnya,entity). - Contoh (
app/Jobs/SendInternalNotification.php):
