使用 Amazon SNS/SQS 在 PHP 中推送通知? [英] Push notifications in PHP using Amazon SNS/SQS?

查看:36
本文介绍了使用 Amazon SNS/SQS 在 PHP 中推送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我想像 Stackoverflow 那样推送评论通知.Amazon SNS/SQS 似乎提供了一个框架来执行此操作,但我很难在网络上找到任何代码/解释来解释hello world"以外的任何内容.

On my site I'd like to do push notifications of comments like Stackoverflow does. 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.

从阅读 AWS SNS/SQS 文档来看,我需要以下内容:

From reading the AWS SNS/SQS documentation it looks like I need the following:

逻辑:

  1. 发表评论/对新问题的回答
  2. 创建主题(仅限第一条评论/回答)
  3. 发布消息
  4. 订阅主题

发表评论的页面上的 PHP (http://mysite.com/postCommentOrAnswer.php):

PHP on the page where comments are posted (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 (http://mysite.com/notificationsReceiver.php):

PHP on the page where notifications are received (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?

推荐答案

你的评论暗示你不喜欢 SQS,所以我用 MySQL 解决方案来回答.

Your comment implied that you are not wedded to SQS, so I am answering with a MySQL solution.

除非您处理的流量太大以至于消息实际上会排队,否则我建议您只使用简单的 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:

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.

我有超过 10,000 名用户,到目前为止,这种方法尚未证明是该网站的瓶颈.我也不指望它会很快出现.

I'm at well over 10,000 users and so far this approach has not proven to be a bottleneck for the site. I don't expect it to anytime soon, either.

这篇关于使用 Amazon SNS/SQS 在 PHP 中推送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆