dot Redis 8 来了——而且是开源的

了解更多

如何使用 Redis 构建您自己的私有社交媒体平台

对很多人来说,社交媒体体验已经变成一种压力体验。 新闻提要已成为赞助内容的公告板。 状态、评论和任何在线互动都会让用户接触到焦躁不安的键盘侠。 有人可能会说,社交媒体已经变得名不副实

现在,随着社交媒体的大佬们继续犹豫不决,这个Launchpad App 已经直面这个问题,创建了自己的社交媒体平台 Letus。 这个应用程序的核心价值是为用户提供一个社交媒体平台,促进朋友、家人和工作同事之间更自然、更有意义的互动。

从头到尾,这个应用程序都依赖于 RedisGraph 的强大、速度和灵活性。 让我们来看看这个应用程序是如何创建的。

但在我们深入研究之前,请务必浏览一下 Redis Launchpad,了解一系列令人兴奋的应用程序供您参与。

https://www.youtube.com/embed/MDlebl9tkd0
  1. 你会建造什么?
  2. 你需要什么?
  3. 架构
  4. 入门
  5. 它是如何工作的

1. 你会建造什么?

您将构建一个社交媒体平台,保护用户免受网络喷子的攻击,并让他们摆脱赞助内容。 这个移动应用程序将显示您的朋友、家人或您在 Letus 上建立联系的任何人发布的帖子。

不会有任何神秘的算法让你接触到不想要的连接和广告。 相反,Letus 将让您完全控制您将看到的内容。 下面我们将详细介绍将此应用程序变为现实所需采取的 A-Z 步骤,以及所需的组件。


2. 你需要什么?

RedisGraph用于稀疏矩阵来表示图中的邻接矩阵,并使用线性代数来查询图。

Google Cloud Platform (GCP)用作公共云供应商,用于在此项目中对文本进行分类和分析情感。

Azure Functions用作无服务器解决方案,以减少代码编写、维护基础设施并节省成本。

Google Identity用作客户身份和访问管理 (CIAM) 平台,通过基于 Web 的 Oauth 2.0 对用户进行身份验证。

Expo:移动开发人员用于构建应用程序

Firebase:后端即服务

3. 架构

4. 入门

先决条件

步骤 1:克隆存储库

$ git clone https://github.com/redis-developer/letus

步骤 2. 安装 Expo CLI

$ cd LetusApp
$ npm install --global expo-cli

步骤 3. 安装配置插件

可以使用 expo install 命令自动将带有配置插件的节点模块添加到项目的 Expo 配置中

$ npm install
$ expo install

对于本地开发环境

对于本地开发,可以使用 redismod Docker 容器运行 RedisGraph

$ docker run -d -p 6379:6379 redislabs/redismod

您可以仅在本地运行 Expo Go App 以使用和开发 react native 应用程序。 通过访问现有的(免费)firebase 身份验证和现有的(免费)Azure 函数,这些函数仅为 DEV 部署。

  • 将 env.dev.example 的内容复制到您的 .env 文件中
  • 通过 expo start 启动应用程序
  • 通过移动模拟器注册一个新用户,您就可以开始了!
开发人员工具在 http://localhost:19002 上运行
在浏览器中打开开发人员工具…
启动 Metro Bundler

Metro 正在等待 exp://192.168.1.9:19000

对于生产环境

步骤 4. 设置环境变量

Create a .env.local file to store your local configuration
Set the value of LETUS_API_URL to the location of your API (can use .env if local functions)
Set the value of GOOGLE_WEB_CLIENT_ID to use a Web OAUTH credential created in your Google Identity account.
Set value of firebase config in the .env or .env.local file
FIREBASE_API_KEY=
FIREBASE_AUTH_DOMAIN=
FIREBASE_PROJECT_ID=
FIREBASE_STORAGE_BUCKET=
FIREBASE_MEASSGE_SENDER_ID=
FIREBASE_APP_ID=

步骤 5. 启动开发服务器

$ expo start

按 i 启动 iOS 模拟器或 a 启动 Android 模拟器。 (注意:Android 模拟器需要 Android Studio 和 sdk 设置)

步骤 6:设置 Letus 函数

此项目使用 Azure Functions,可以直接在您的本地开发中访问。 如果您希望将这些函数发布到云端,您必须创建您自己的 Azure 帐户提示:有一个非常有用的 VS Code 扩展 来帮助您管理 Azure 项目和函数。

  • Azure 函数可以在本地运行。 推荐的选项是使用 VS Code 扩展,然后使用 f5 在调试模式下启动
  • 配置以下环境变量(在 local.settings.json 中或在 Azure 中)
    • RedisGraph 数据库的主机
REDIS_HOST
  • RedisGraph 数据库使用的端口
REDIS_PORT
  • 默认 Redis 用户的密码
REDIS_PASS
  • 要使用的图的名称
REDIS_GRAPH
  • 用于语言处理的 Google Cloud Platform API 密钥(AnalyzeSentiment 和 ClassifyText)
GCP_API_KEY
  • 将您的 serviceAccount.json 文件添加到 LetusFunctions/shared 以配置 API 的 firebase-admin

步骤 7:设置身份验证

步骤 8:设置 RedisGraph

单击 /try-free/ 并设置带有 RedisGraph 的 Redis Enterprise 云数据库,如下所示

Redis Insight
  • GetPosts – Letus 的核心。 使用 cypher 遍历当前用户的网络并返回相关的、最新的帖子
    MATCH (me:Person {userid: $userid}) 
MATCH (me:Person {userid: $userid}) 
  OPTIONAL MATCH (me)-[:ignores]->(ign:IgnoreSetting) 
  WITH me, ign  
  MATCH (poster:Person)-[:posted]->(post:Post) 
  WHERE (poster = me OR (poster)-[:friended]-(me)) 
  AND (NOT (post)-[:inCategory]->(:Category {name:ign.category}) AND NOT (post)<-[:posted]-(:Person {userid:ign.poster})) 
  WITH post, poster, me 
  OPTIONAL MATCH (post)-[:hasComment]->(comment:Comment)<-[:commented]-(commenter:Person) 
  WHERE commenter = me OR (commenter)-[:friended]-(me) 
  WITH me, post, poster, collect(comment) as comments, collect(commenter) as commenters 
  ORDER BY post.created DESC 
  SKIP $skip
 LIMIT $limit
  RETURN post, poster, comments, commenters
  • CreatePost – 创建一个新帖子,其中包含来自 NLP 处理的附加信息。
MATCH (me:Person {userid:$userid}) ${categories
.map(
(cat, index) =>
    'MERGE (cat' + index + ':Category {name: $cat' + index + '})'
)
.join(
' '
)}  
MERGE (sentiment:Sentiment {name: $sentiment}) 
CREATE (me)-[:posted]->(post:Post {text:$text,created:$now}) 
MERGE (post)-[:hasSentiment]->(sentiment) ${categories
.map((cat, index) => 'MERGE (post)-[:inCategory]->(cat' + index + ')')
.join(' ')} 
RETURN post
  • AddComment – 将您的评论添加到连接的帖子。
MATCH (me:Person), (post:Post) 
WHERE me.userid = $userid 
AND ID(post) = $onPost 
CREATE (me)-[:commented]->(comment:Comment {text:$text,created:$now}) 
CREATE (post)-[:hasComment]->(comment) 
RETURN post
  • AddFriend – 从您的方向添加新的连接。

注意:单向 :friended 关系确定待处理的好友请求

MATCH (me:Person { userid: $userid }) 
MATCH (them:Person { userid: $themid })
MERGE (me)-[:friended]->(them)
RETURN me, them
  • GetFriendRequests – 返回 :friended 您但未被您 :friended 的 Person 节点的列表。
MATCH (me:Person {userid: $userid}) 
WITH me 
MATCH (them:Person)-[:friended]->(me) 
WHERE NOT (me)-[:friended]->(them) 
RETURN them
  • AddIgnoreSetting – 向用户添加新的 IgnoreSetting
MATCH (me:Person { userid: $userid }) 
MERGE (me)-[:ignores]->(ignore:IgnoreSetting {poster:$themid, category: $category, sentiment: $sentiment})
RETURN me, ignore
  • FindFriends – 按名称搜索未添加好友的人
MATCH (them:Person) 
MATCH (me:Person {userid:$userid}) 
WHERE NOT them.userid = $userid 
AND them.name STARTS WITH $name 
AND NOT (me)-[:friended]->(them) 
RETURN them

NLP 命令

使用 GCP Natural Language 的免费层,我们将情感分析和文本分类应用于系统中发布的所有帖子。

  • AnalyzeSentiment
    const sslCreds = getApiKeyCredentials();
const sslCreds = getApiKeyCredentials();
const client = new language.LanguageServiceClient({ sslCreds });
const document = {
content: text,
 type: 'PLAIN_TEXT',
};
const [result] = await client.analyzeSentiment({ document: document });
sentiment = mapSentiment(result.documentSentiment.score || 0);
  • ClassifyText – 返回匹配的 NLP 类别列表
    let content = text;
// GCP requires 20 words
// we pad with prepositions if between 10-19 words for max coverage
if (len < 20) {
  content = [...content.split(' '), ...preps.slice(0, 20 - len)].join(' ');
}
const sslCreds = getApiKeyCredentials();
const client = new language.LanguageServiceClient({ sslCreds });
 
const document = {
  content,
  type: 'PLAIN_TEXT',
};
const [result] = await client.classifyText({ document: document });
categories = result.categories || [];

结论:从社交媒体中删除网络喷子和广告

随着社交媒体继续融入日常生活,网络喷子行为仍然像以往一样普遍。 Facebook、Twitter、Instagram 甚至 Linkedin 都已成为焦躁不安的键盘侠的狩猎场。

然后,您会不断地受到赞助内容的轰炸,这些内容的利益优先于用户。 但是通过使用 Redis,这个市场应用程序能够为移动用户创建一个社交媒体平台,消除了这两种危害。

RedisGraph 的强大功能和速度对于构建 Letus 至关重要,因为它允许应用程序以最高的效率查询数据。 从最初只是一个简单的想法开始,Redis 能够将这个应用程序变为现实……但这还不是全部。

每天,人们都在利用 Redis 的奇迹来创建令人兴奋的应用程序,这些应用程序正在对日常生活产生影响。 您如何使用 Redis 来帮助改善社会?

如需更多灵感,您可以访问 Redis Launchpad,您可以在那里找到各种创新应用程序。 同样,您也可以通过 点击此处 了解有关此应用程序的更多信息。

谁构建了这个应用程序?

Matt Pileggi

Matt 在 Web 开发方面拥有超过 20 年的经验,现在是 Games24x7 的首席工程师。

务必通过 查看他的页面,随时了解 Matt 在 Github 上的所有最新项目。