未加星标

Build Real-time Apps in PHP using WebSockets, Laravel and Twilio Sync

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

Build Real-time Apps in PHP using WebSockets, Laravel and Twilio Sync

Twilio Sync is a powerful API which allows us at Momentum , an online travel agency, to keep our dashboards up to date with its WebSockets entities. Pairing it with Laravel ’s Broadcasting feature, however, gave us the ability to efficiently dispatch, queue, serialize and log our Twilio Sync calls from Laravel. In this post, I will walk you through how you can extend Laravel’s framework to make use of Twilio Sync natively.

Additionally, from this tutorial we will:

Learn about Twilio Sync Streams Implement Twilio as a Service Container within Laravel Creating a Driver for Laravel’s Create a REST API for Twilio callbacks Require the Twilio SDK

First, we will need a Laravel project . Version 5 is recommended. For this walkthrough, I will be using the latest version available (at the time of writing, 5.7). If you're using Valet or are comfortable with command line installs, run the following command:

$ mkdir project-name $ cd project-name $ laravel new

Next we need to includeTwilio’sphp SDKas a Composer dependency in order to make use of its RESTful Client wrapper in our project. In the terminal, navigate to your Laravel project and run the following command:

$ composer require twilio/sdk Enable Support for Laravel Broadcasting Laravel Broadcasting is a feature offered by the framework which gives you the ability to send events from your backend application to your WebSocket connections on the front end of your application. By default, broadcasting is disabled in Laravel, so if you've never used it before, or you are starting with a brand new project, make sure the Broadcaster service provider has been uncommented inside the config/app.php file.
Build Real-time Apps in PHP using WebSockets, Laravel and Twilio Sync

Finally, we want to set up a queue that will listen to incoming broadcast events. Out of the box, that feature is supported synchronously, but if you were willing to use another queue connection, make sure to have php artisan queue:work running in the background to ensure events are being processed.

Twilio / Twilio Sync as a Service Container

Our primary goal is to allow our Laravel App to integrate Twilio Sync’s flexible entities, while making use of the Broadcasting’s serializable and dispatchable channel events. For Laravel to utilize Twilio Sync as a broadcaster, we need to provide its REST API as an injectable dependency. Therefore, it is necessary to bind Twilio Sync as a service container in order to make the class injectable and its configured instance reusable. Laravel offers more information on understanding service containers in their docs .

The first step in binding Twilio Sync is toexecute the Artisan Command to create a Twilio service provider.In your command line run:

$ php artisan make:provider TwilioServiceProvider

This will create a file in the app/ P roviders folder named TwilioServiceProvider.php .


Build Real-time Apps in PHP using WebSockets, Laravel and Twilio Sync

Let’s navigate into that file and add the following namespaces:

<?php namespace App\Providers; use Illuminate\Support\Facades\Broadcast;

You will notice that the register () function has already been generated for us. In that method I’ve decided to create a singleton, which will turn the constructor I providedit into an injectable dependency for the rest of ourapplication.Update your register() function to include the following code:

<?php /** * Register services. * * @return void */ public function register() { $this->app->singleton( 'sync', function () { $client = new \Twilio\Rest\Client( env('TWILIO_ACCOUNT_SID'), env('TWILIO_AUTH_TOKEN') ); return $client->sync->services( env('TWILIO_SYNC_SERVICE_SID') ); } ); }

The name of my singleton in this example is "sync", which means that whenever the app creates a service with that name, the constructor on the second argument will execute in order to return an instance of ServiceContext from TwilioSync’s API Client SDK. For any subsequent calls, the application will refer to the same instance across the application’s runtime, allowing us to make use of the same configuration without declaring it again every subsequenttime.

The parameters are configured using environment variables which are set in our .env file. Therefore, we will need to include these in order for our ServiceContext to return an appropriate REST object to work with.

TWILIO_ACCOUNT_SID=ACxxx TWILIO_AUTH_TOKEN=XXXXX TWILIO_SYNC_SERVICE_SID=ISxxx TWILIO_SYNC_API_KEY=SKXXXX TWILIO_SYNC_API_SECRET=XXXXX

You can get your Account SIDand Auth Token from yourTwilio Console. As for the Service SID, this can be set to“default”,or to the Service SID of your choice if you ever intend to manage multiple services. Finally, you can create yourself a Twilio API key from the Twilio Sync runtime configuration page. This will be used to sign the access token used by our frontend client that will receive the events we will publish.

Once that is done, we can proceed to register our new ServiceProvider inside your config/app.php file:

<?php /* * Application Service Providers... */ App\Providers\AppServiceProvider::class, App\Providers\AuthServiceProvider::class, App\Providers\TwilioServiceProvider::class, // <----- This will register the driver we have created App\Providers\BroadcastServiceProvider::class, // <----- Make sure this is uncommented App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, Creating the Broadcaster

Now that the base requirements have been fulfilled, wecan start building the broadcasting engine. In order to make it compatible with Laravel, let’s use the existing drivers inside the project and created a new file named app/Broadcasters/SyncBroadcaster.php .

We’llextend that class to Laravel’s Broadcaster’s abstract class in order to build a proper driver that will be extended in the framework.

<?php namespace App\Broadcasters; use Illuminate\Broadcasting\Broadcasters\Broadcaster; use Twilio\Rest\Sync\V1\ServiceContext; use Illuminate\Support\Arr; class SyncBroadcaster extends Broadcaster { /** * @var ServiceContext */ protected $sync; /** * Create a new broadcaster instance. * * @param ServiceContext $sync * @return void */ public function __construct( ServiceContext $sync ) { $this->sync = $sync; } } The constructor directly makes use of a ServiceContext. This is set to a property in the class which I’ve named $sync . That property will be used in the followingfunctions in

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

分页:12
转载请注明
本文标题:Build Real-time Apps in PHP using WebSockets, Laravel and Twilio Sync
本站链接:https://www.codesec.net/view/620851.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(9)