简体   繁体   English

使用Amazon SNS / SQS在PHP中推送通知?

[英]Push notifications in PHP using Amazon SNS/SQS?

On my site I'd like to do push notifications of comments like Stackoverflow does. 在我的网站上,我想做一些像Stackoverflow这样的评论推送通知。 Amazon SNS/SQS seems to provide a framework to do this but I'm having difficulty finding any code/explanation on the web for anything beyond a "hello world" equivalent. 亚马逊SNS / SQS似乎提供了一个框架来实现这一点,但我很难在网上找到任何超出“hello world”等级的代码/解释。

From reading the AWS SNS/SQS documentation it looks like I need the following: 从阅读AWS SNS / SQS文档看起来我需要以下内容:

logic: 逻辑:

  1. post comment/answer to a new question 发表评论/回答新问题
  2. create topic (for first comment/answer only) 创建主题(仅限第一个评论/答案)
  3. publish message 发布消息
  4. subscribe to topic 订阅主题

PHP on the page where comments are posted (http://mysite.com/postCommentOrAnswer.php): 发布评论的页面上的PHP(http://mysite.com/postCommentOrAnswer.php):

$comment=$_POST['comment']; //posted comment
require_once 'application/third_party/AWSSDKforPHP/sdk.class.php';
$sns = new AmazonSNS();

$response = $sns->create_topic('SO-like-question-12374940'); //create topic

$response = $sns->publish(
  'arn:aws:sns:us-east-1:9876543210:SO-like-question-12374940',
  $comment
);  //publish comment

$response = $sns->subscribe(
  'arn:aws:sns:us-east-1:9876543210:SO-like-question-12374940',
  'https ',
  'https://mysite.com/notificationsReceiver'
); // Subscribe to notifications

PHP on the page where notifications are received (http://mysite.com/notificationsReceiver.php): 收到通知的页面上的PHP(http://mysite.com/notificationsReceiver.php):

no idea, thoughts?

Obviously, this is not close to being a complete demonstration and probably has some incorrect function calls but I was wondering if someone might be able to help build upon this? 显然,这并不是一个完整的演示,可能有一些不正确的函数调用,但我想知道是否有人可以帮助建立这个?

Your comment implied that you are not wedded to SQS, so I am answering with a MySQL solution. 您的评论意味着您没有与SQS结合,所以我正在回答MySQL解决方案。

Unless you're dealing with so much traffic that messages would actually ever get queued, I'd recommend just a simple MySQL table approach. 除非你处理的是如此多的流量,否则消息实际上会排队,我建议只使用简单的MySQL表方法。

I have a site with a MySQL notifications table that looks like this: 我有一个带有MySQL通知表的站点,如下所示:

CREATE TABLE `notification` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `user_id` INT(11) NOT NULL,
    `notification_type` ENUM('inline','popup') NOT NULL DEFAULT 'inline',
    `html` TEXT NOT NULL,
    `entered_date` DATETIME NOT NULL,
    `display_date` DATETIME NOT NULL,
    `show_once` TINYINT(1) NOT NULL DEFAULT '0',
    `closable` TINYINT(1) NOT NULL DEFAULT '1',
    `destroy_on_close` TINYINT(1) NOT NULL DEFAULT '1',
    PRIMARY KEY (`id`),
    INDEX `user_id` (`user_id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM

This table is checked upon page load and the proper notification is displayed according to the notification data. 在页面加载时检查该表,并根据通知数据显示适当的通知。 Insertion is done as various actions or events occur on the website. 插入是在网站上发生各种动作或事件时完成的。

I'm at well over 10,000 users and so far this approach has not proven to be a bottleneck for the site. 我有超过10,000名用户,到目前为止,这种方法还没有被证明是该网站的瓶颈。 I don't expect it to anytime soon, either. 我也不希望它很快到来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM