Nexmo / nexmo-laravel

Add Vonage functionality such as SMS and voice calling to your Laravel app with this Laravel Service Provider.
MIT License
318 stars 79 forks source link

max 1 request per second in Canada/US #19

Closed vwasteels closed 6 years ago

vwasteels commented 6 years ago

Hello,

this a question.

In this documentation : https://help.nexmo.com/hc/en-us/articles/204024183-Canada-direct-route-

It says that in Canada / US , I need to buy a long virtual number to be able to send SMS to my customers.

It also says that long virtual numbers can't exceed 1 SMS sending per second.

My question is : is that handled in this package ?

Thank you !!

mheap commented 6 years ago

Hi @vwasteels,

The answer is somewhere in the middle. If you try and send an SMS too quickly, the Nexmo API will respond with a rate limiting error. This is caught by the library and an exception is thrown. However, the library does not buffer up SMS requests and send them at a rate of one per second

Hope that helps!

vwasteels commented 6 years ago

yes, it is a complete answer :)

vwasteels commented 6 years ago

For information, this is how I fixed this :

I used the Laravel\Queue system,

This is the Job created for this :

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

use Nexmo;
use App;

class ProcessSMS implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $destination;
    protected $message;

    public $tries = 2;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($destination, $message)
    {
        $this->destination = $destination;
        $this->message = $message;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
      $throttler = App::make('throttler');
      $throttler->throttle('nexmo', 1, 5000);

      Nexmo::message()->send([
        'to'   => $this->destination,
        'from' => config('nexmo.from'),
        'text' => $this->message
      ]);
    }
}

and used it combined with a Throttling package : https://github.com/davedevelopment/stiphle

and added the throttler in my AppServiceProvider, in the boot method :

$this->app->singleton('throttler', '\Stiphle\Throttle\TimeWindow');

This will add a delay of 5 seconds between each message. And max tries at 2.

Hope it helps someone !