Table of Contents
产品分析
简书
知乎
消息的三种分类
提醒的语言分析
消息的两种获取方式
订阅
聚合
五个实体
行为分解
模型设计
Notify
UserNotify
Subscription
SubscriptionConfig
配置文件 NotifyConfig
服务层 NotifyService
NotifyService拥有以下方法:
各方法的处理逻辑如下:
时序图
提醒的订阅、创建、拉取
公告的创建、拉取
信息的创建
Home Backend Development PHP Tutorial 讯息系统设计与实现

讯息系统设计与实现

Jun 13, 2016 pm 12:29 PM
gt notify type

消息系统设计与实现

文/JC_Huang(简书作者)
原文链接:http://www.jianshu.com/p/f4d7827821f1
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

产品分析

首先我们来看一下市场上关于消息的实现是怎么样的。

简书

简书的消息系统主要分了两种

  • 简信
  • 提醒

简信
简信的性质其实跟私信是一样的,是用户发送给用户的一则消息,有具体的信息内容。


简书简信

提醒
而提醒,则是系统发送的一则消息,其文案格式是固定的,并且对特殊对象一般拥有超链接。


简书提醒

知乎

知乎跟简书一样,主要分了两种:

  • 私信
  • 消息

私信
跟简书一样,使用户发送给用户的一则消息,也可以是管理员发送给用户的消息。


知乎私信

消息
知乎的消息比简书的提醒有过之而无不及,知乎会对多条相似的消息进行聚会,以达到减轻用户阅读压力的体验。


知乎消息

消息的三种分类

通过两种产品的简单分析,得出他们的消息有两种分类,在这基础上,我们再加上一种:公告。
公告的主要性质是系统发送一则含有具体内容的消息,站内所有用户都能读取到这条消息。
所以,消息有三种分类:

  1. 公告 Announce
  2. 提醒 Remind
  3. 私信 Message

提醒的语言分析

我们从简书取一组提醒样本:

  • 3dbe1bd90774 关注了你
  • magicdawn 喜欢了你的文章 《单点登录的三种实现方式》
  • 无良程序 喜欢了你的文章 《基于RESTful API 怎么设计用户权限控制?》
  • alexcc4 喜欢了你的文章 《在Nodejs中贯彻单元测试》
  • 你在《基于RESTful API 怎么设计用户权限控制?》中收到一条 cnlinjie 的评论
  • 你的文章《Session原理》已被加入专题 《ios开发》

分析句子结构,提醒的内容无非就是

「谁对一样属于谁的事物做了什么操作」
「someone do something in someone's something」

someone = 提醒的触发者,或者发送者,标记为sender
do something = 提醒的动作,评论、喜欢、关注都属于一个动作,标记为action
something = 提醒的动作作用对象,这就具体到是哪一篇文章,标记为target
someone's = 提醒的动作作用对象的所有者,标记为targetOwner

这就清楚了,sender和targetOwner就是网站的用户,而target是具体到哪一篇文章,如果提醒的对象不仅仅局限于文章,还有其他的话,就需要增加一项targetType,来标记目标是文章还是其他的什么。而action,则是固定的,整个网站会触发提醒的动作可能就只有那几样:评论、喜欢、关注.....(或者其他业务需要提醒的动作)

消息的两种获取方式

  • 推 Push
  • 拉 Pull

以知乎为例
推的比较常见,需要针对某一个问题维护着一张关注者的列表,每当触发这个问题推送的条件时(例如有人回答问题),就把这个通知发送给每个关注者。

拉的相对麻烦一点,就是推的反向,例如每个用户都有一张关注问题的列表,每当用户上线的时候,对每个问题进行轮询,当问题的事件列表出现了比我原本时间戳大的信息就进行拉取。

而我们则根据消息的不同分类采用不同的获取方式
通告和提醒,适合使用拉取的方式,消息产生之后,会存在消息表中,用户在某一特定的时间根据自己关注问题的表进行消息的拉取,然后添加到自己的消息队列中,

信息,适合使用推的方式,在发送者建立一条信息之后,同时指定接收者,把消息添加到接收者的消息队列中。

订阅

根据提醒使用拉取的方式,需要维护一个关注某一事物的列表。
这种行为,我们称之为:「订阅」Subscribe

一则订阅有以下三个核心属性

  • 订阅的目标 target
  • 订阅的目标类型 targetType
  • 订阅的动作 action

比如我发布了一篇文章,那么我会订阅文章《XXX》的评论动作,所以文章《XXX》每被人评论了,就需要发送一则提醒告知我。

订阅的规则还可以扩展
我喜欢了一篇文章,和我发布了一篇文章,订阅的动作可能不一样。
喜欢了一篇文章,我希望我订阅这篇文章更新、评论的动作。
而发布了一篇文章,我希望我只是订阅这篇文章的评论动作。

这时候就需要多一个参数:subscribReason
不同的subscribReason,对应着一个动作数组,
subscribReason = 喜欢,对应着 actions = [更新,评论]
subscribReason = 发布,对应着 actions = [评论]

订阅的规则还还可以扩展
用户可能会有一个自己的订阅设置,比如对于所有的喜欢的动作,我都不希望接收。
比如Knewone的提醒设置


Knewone提醒设置

所以我们需要再维护一个表:SubscriptionConfig,来存放用户的提醒设置。
并且,当用户没有提醒设置的时候,可以使用系统提供的一套默认设置:defaultSubscriptionConfig

聚合

如果我发布了一篇文章《XXX》,在我不在线的时候,被评论了10遍,当我一上线的时候,应该是收到十条信息类似于:「谁谁谁评论了你的文章《XXX》」?
还是应该收到一条信息:「甲、乙、丙、丁...评论了你的文章《XXX》」?

知乎在聚合上做的很优秀,要知道他们要实现这个还是挺有技术的:
知乎的消息机制,在技术上如何设计与规划?
网站的消息(通知)系统一般是如何实现的?

关于这部分功能,我们还没有具体的实现方法,暂时也无法讲得更加详细。⊙﹏⊙

五个实体

通过上面的分析,大概知道做这个消息系统,需要哪些实体类:

  1. 用户消息队列 UserNotify
  2. 用户 User
  3. 订阅 Subscription
  4. 订阅设置 SubscriptionConfig
  5. 消息 Notify
    • 通告 Announce
    • 提醒 Remind
    • 信息 Message

行为分解

说了这么多,整理一下整个消息流程的一些行为:

  • 系统或者管理员,创建消息
    • createNotify (make announce | remind | message)
  • 用户,订阅消息,取消订阅
    • subscribe, cancelSubscription
  • 用户管理订阅设置
    • getSubscriptionConfig, updateSubscriptionConfig
  • 用户,拉取消息
    • pullNotify (pull announce | remind | message | all)
  • 用户,查询消息队列
    • getUserNotify(get announce | remind | message | all)
  • 用户阅读消息
    • read

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

模型设计

Notify

<code class="javascript">id            : {type: <span class="hljs-string">'integer', primaryKey: <span class="hljs-literal">true},        <span class="hljs-comment">// 主键content     : {type: <span class="hljs-string">'text'},    <span class="hljs-comment">// 消息的内容type        : {type: <span class="hljs-string">'integer', required: <span class="hljs-literal">true, enum: [<span class="hljs-number">1, <span class="hljs-number">2, <span class="hljs-number">3]},  <span class="hljs-comment">// 消息的类型,1: 公告 Announce,2: 提醒 Remind,3:信息 Messagetarget      : {type: <span class="hljs-string">'integer'},    <span class="hljs-comment">// 目标的IDtargetType  : {type: <span class="hljs-string">'string'},    <span class="hljs-comment">// 目标的类型action      : {type: <span class="hljs-string">'string'},    <span class="hljs-comment">// 提醒信息的动作类型sender      : {type: <span class="hljs-string">'integer'},    <span class="hljs-comment">// 发送者的IDcreatedAt    : {type: <span class="hljs-string">'datetime', required: <span class="hljs-literal">true}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

Save Remind
消息表,我们需要targettargetType字段,来记录该条提醒所关联的对象。而action字段,则记录该条提醒所关联的动作。
比如消息:「小明喜欢了文章」
则:

<code class="javascript">target = <span class="hljs-number">123,  <span class="hljs-comment">// 文章IDtargetType = <span class="hljs-string">'post',  <span class="hljs-comment">// 指明target所属类型是文章sender = <span class="hljs-number">123456  <span class="hljs-comment">// 小明ID</span></span></span></span></span></span></code>
Copy after login

Save Announce and Message
当然,Notify还支持存储公告和信息。它们会用到content字段,而不会用到targettargetTypeaction字段。

UserNotify

<code class="javascript">id            : {type: <span class="hljs-string">'integer', primaryKey: <span class="hljs-literal">true},        <span class="hljs-comment">// 主键isRead      : {type: <span class="hljs-string">'boolean', required: <span class="hljs-literal">true},   user        : {type: <span class="hljs-string">'integer', required: <span class="hljs-literal">true},  <span class="hljs-comment">// 用户消息所属者notify      : {type: <span class="hljs-string">'integer', required: <span class="hljs-literal">true}   <span class="hljs-comment">// 关联的NotifycreatedAt    : {type: <span class="hljs-string">'datetime', required: <span class="hljs-literal">true}</span></span></span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

我们用UserNotify来存储用户的消息队列,它关联一则提醒(Notify)的具体内容。
UserNotify的创建,主要通过两个途径:

  1. 遍历订阅(Subscription)表拉取公告(Announce)和提醒(Remind)的时候创建
  2. 新建信息(Message)之后,立刻创建。

Subscription

<code class="javascript">target      : {type: <span class="hljs-string">'integer', required: <span class="hljs-literal">true},    <span class="hljs-comment">// 目标的IDtargetType  : {type: <span class="hljs-string">'string', required: <span class="hljs-literal">true},    <span class="hljs-comment">// 目标的类型action      : {type: <span class="hljs-string">'string'},   <span class="hljs-comment">// 订阅动作,如: comment/like/post/update etc.user        : {type: <span class="hljs-string">'integer'},createdAt    : {type: <span class="hljs-string">'datetime', required: <span class="hljs-literal">true}</span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

订阅,是从Notify表拉取消息到UserNotify的前提,用户首先订阅了某一个目标的某一个动作,在此之后产生这个目标的这个动作的消息,才会被通知到该用户。
如:「小明关注了产品A的评论」,数据表现为:

<code class="javascript">target: <span class="hljs-number">123,  <span class="hljs-comment">// 产品A的IDtargetType: <span class="hljs-string">'product',action: <span class="hljs-string">'comment',user: <span class="hljs-number">123  <span class="hljs-comment">// 小明的ID</span></span></span></span></span></span></code>
Copy after login

这样,产品A下产生的每一条评论,都会产生通知给小明了。

SubscriptionConfig

<code class="javascript">action: {type: <span class="hljs-string">'json', required: <span class="hljs-literal">true},   <span class="hljs-comment">// 用户的设置user: {type: <span class="hljs-string">'integer'}</span></span></span></span></code>
Copy after login

不同用户可能会有不一样的订阅习惯,在这个表中,用户可以统一针对某种动作进行是否订阅的设置。而默认是使用系统提供的默认配置:

<code class="javascript">defaultSubscriptionConfig: {  <span class="hljs-string">'comment'   : <span class="hljs-literal">true,    <span class="hljs-comment">// 评论  <span class="hljs-string">'like'      : <span class="hljs-literal">true,    <span class="hljs-comment">// 喜欢}</span></span></span></span></span></span></code>
Copy after login

在这套模型中,targetTypeaction是可以根据需求来扩展的,例如我们还可以增加多几个动作的提醒:hate被踩、update被更新....诸如此类。

配置文件 NotifyConfig

<code class="javascript"><span class="hljs-comment">// 提醒关联的目标类型targetType: {  PRODUCT : <span class="hljs-string">'product',    <span class="hljs-comment">// 产品  POST    : <span class="hljs-string">'post'    <span class="hljs-comment">// 文章},<span class="hljs-comment">// 提醒关联的动作action: {  COMMENT   : <span class="hljs-string">'comment',  <span class="hljs-comment">// 评论  LIKE      : <span class="hljs-string">'like',     <span class="hljs-comment">// 喜欢},<span class="hljs-comment">// 订阅原因对应订阅事件reasonAction: {  <span class="hljs-string">'create_product'  : [<span class="hljs-string">'comment', <span class="hljs-string">'like']  <span class="hljs-string">'like_product'    : [<span class="hljs-string">'comment'],  <span class="hljs-string">'like_post'       : [<span class="hljs-string">'comment'],},<span class="hljs-comment">// 默认订阅配置defaultSubscriptionConfig: {  <span class="hljs-string">'comment'   : <span class="hljs-literal">true,    <span class="hljs-comment">// 评论  <span class="hljs-string">'like'      : <span class="hljs-literal">true,    <span class="hljs-comment">// 喜欢}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

服务层 NotifyService

NotifyService拥有以下方法:

  • createAnnounce(content, sender)
  • createRemind(target, targetType, action, sender, content)
  • createMessage(content, sender, receiver)
  • pullAnnounce(user)
  • pullRemind(user)
  • subscribe(user, target, targetType, reason)
  • cancelSubscription(user, target ,targetType)
  • getSubscriptionConfig(userID)
  • updateSubscriptionConfig(userID)
  • getUserNotify(userID)
  • read(user, notifyIDs)

各方法的处理逻辑如下:

createAnnounce(content, sender)

  1. 往Notify表中插入一条公告记录

createRemind(target, targetType, action, sender, content)

  1. 往Notify表中插入一条提醒记录

createMessage(content, sender, receiver)

  1. 往Notify表中插入一条信息记录
  2. 往UserNotify表中插入一条记录,并关联新建的Notify

pullAnnounce(user)

  1. 从UserNotify中获取最近的一条公告信息的创建时间: lastTime
  2. lastTime作为过滤条件,查询Notify的公告信息
  3. 新建UserNotify并关联查询出来的公告信息

pullRemind(user)

  1. 查询用户的订阅表,得到用户的一系列订阅记录
  2. 通过每一条的订阅记录的targettargetTypeactioncreatedAt去查询Notify表,获取订阅的Notify记录。(注意订阅时间必须早于提醒创建时间)
  3. 查询用户的配置文件SubscriptionConfig,如果没有则使用默认的配置DefaultSubscriptionConfig
  4. 使用订阅配置,过滤查询出来的Notify
  5. 使用过滤好的Notify作为关联新建UserNotify

subscribe(user, target, targetType, reason)

  1. 通过reason,查询NotifyConfig,获取对应的动作组:actions
  2. 遍历动作组,每一个动作新建一则Subscription记录

cancelSubscription(user, target ,targetType)

  1. 删除usertargettargetType对应的一则或多则记录

getSubscriptionConfig(userID)

  1. 查询SubscriptionConfig表,获取用户的订阅配置

updateSubscriptionConfig(userID)

  1. 更新用户的SubscriptionConfig记录

getUserNotify(userID)

  1. 获取用户的消息列表

read(user, notifyIDs)

  1. 更新指定的notify,把isRead属性设置为true

时序图

提醒的订阅、创建、拉取


提醒的订阅、创建、拉取


我们可以在产品创建之后,调用NotifyService.subscribe方法,
然后在产品被评论之后调用NotifyService.createRemind方法,
再就是用户登录系统或者其他的某一个时刻调用NotifyService.pullRemind方法,
最后在用户查询消息队列的时候调用NotifyService.getUserNotify方法。

公告的创建、拉取


公告的创建、拉取


在管理员发送了一则公告的时候,调用NotifyService.createAnnounce方法,
然后在用户登录系统或者其他的某一个时刻调用NotifyService.pullAnnounce方法,
最后在用户查询消息队列的时候调用NotifyService.getUserNotify方法。

信息的创建


信息的创建


信息的创建,只需要直接调用NotifyService.createMessage方法就可以了,
在下一次用户查询消息队列的时候,就会查询这条信息。


 

 

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What are the uses of the Type keyword in Go? What are the uses of the Type keyword in Go? Sep 06, 2023 am 09:58 AM

The usage of the Type keyword in Go includes defining new type aliases or creating new structure types. Detailed introduction: 1. Type alias. Use the "type" keyword to create an alias for an existing type. This alias does not create a new type, but only provides a new name for the existing type. Type aliases can improve code. The readability of the code makes the code clearer; 2. Structure type. Use the "type" keyword to create a new structure type. The structure is a composite type that can be used to define custom types containing multiple fields. etc.

Solve Ubuntu mounting mobile hard disk error: unknown file system type exfat Solve Ubuntu mounting mobile hard disk error: unknown file system type exfat Jan 05, 2024 pm 01:18 PM

An error occurs when ubuntu mounts a mobile hard disk: mount: unknownfilesystemtype'exfat'. The processing method is as follows: Ubuntu13.10 or install exfat-fuse: sudoapt-getinstallexfat-fuseUbuntu13.04 or below sudoapt-add-repositoryppa:relan/exfatsudoapt-getupdatesudoapt-getinstallfuse- exfatCentOS Linux mount exfat format USB disk error solution to load extfa in CentOS

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

Linux type command Linux type command Mar 20, 2024 pm 05:06 PM

In this guide, we will learn more about the &quot;type&quot; command in Linux. Prerequisites: To perform the steps demonstrated in this guide, you need the following components: A properly configured Linux system. See how to create a LinuxVM for testing and learning purposes. Basic understanding of the command line interface The Type command in Linux is different from other Linux-specific commands (for example: ls, chmod, shutdown, vi, grep, pwd, etc.). The &quot;type&quot; command is a built-in Bash function that is displayed as an argument. Information about the command type provided. $type In addition to Bash, other shells (Zsh, Ksh, etc.) also come with

See all articles