yii2-broadcast
, (*1)
Installation
Next steps will guide you through the process of installing yii2-broadcast using composer. Installation is a quick and easy three-step process., (*2)
Install component via composer
Either run, (*3)
composer require --prefer-dist xutl/yii2-broadcast
or add, (*4)
"yuncms/yii2-broadcast": "~1.0.0"
to the require
section of your composer.json., (*5)
Configuring your application
Add following lines to your main configuration file:, (*6)
'components' => [
'broadcast' => [
'class' => 'xutl\broadcast\Broadcast',
'endPoint' => 'http://abcdefg.mns.cn-hangzhou.aliyuncs.com/',
'accessId' => '1234567890',
'accessKey' => '1234567890',
],
//etc
],
Use broadcast
/** @var \xutl\broadcast\Broadcast $broadcast */
$broadcast = Yii::$app->broadcast;
$topicName = 'CreateTopicAndPublishMessageExample';
$broadcast->create($topicName);
$topic = $broadcast->getTopicRef($topicName);
$subscriptionName = "SubscriptionExample";
try{
$res = $topic->subscribe($subscriptionName,'https://www.baidu.com','test.test');
echo "SubscriptionCreated! \n";
}catch (MnsException $e){
echo "CreateSubscriptionFailed: " . $e;
return;
}
$messageBody = "test";
$res = $topic->publishMessage($messageBody, 'test.test');
var_dump($res);
class ApiController extends \yii\web\Controller
{
public function actions()
{
return [
/**
* Returns an access token.
*/
'callback' => [
'class' => \xutl\broadcast\TopicAction::classname(),
'callback'=>[$this, 'callback'],
],
];
}
/**
*
*/
public function callback($params)
{
print_r($params);
}
}
License
This is released under the MIT License. See the bundled LICENSE.md
for details., (*7)