node.js - 如何在 NodeJS Express 应用程序的其他地方使用连接
问题描述
我到处搜索,但找不到解决问题的好方法(我认为女巫更多……通用……我想了解如何以及为什么)。我使用谷歌 pubsub,我的项目中有一项服务。它有两个功能。我需要 app.js 中的文件并收听。但是我怎样才能在应用程序的其他地方收听呢?就像在控制器中说的那样?这是 messageService.ts 代码:
require('dotenv').config();
const {PubSub} = require('@google-cloud/pubsub');
let messageCount = 0;
let projectId = 'xxx';
let topicName = 'xxxt';
let subscriptionName = 'xxx';
// Instantiates a client
const pubsub = new PubSub({projectId});
// Creates a new topic
const topic = pubsub.topic(topicName);
const subscription = topic.subscription(subscriptionName);
module.exports = {
listenForMessages : () => {
const messageHandler = message => {
console.log(`Received message ${message.id}:`);
console.log(`\tAgresso Data: ${message.data}`);
//console.log(`\tAttributes: ${message.attributes}`);
messageCount += 1;
message.ack();
};
subscription.on('message', messageHandler);
},
sendTestMessage : () => {
let messageObject = {
action: 'insert',
objectType: 'resource',
objectId: 12456,
stamp: Date.now()
}
topic.publish(Buffer.from(JSON.stringify(messageObject)));
console.log('sent test message') ;
}
}
解决方案
您的subscription
常量不一定只能在此文件中使用。
我建议您采用不同的方法/架构。一个你有一个文件PubSubConnection.ts
,你可以在其中进行整个 PubSub/topic/subscription 实例化并在整个项目中共享它。这样您就不必在每次想要使用它时重新创建一个新主题。甚至可以分解为任何主题。
例子:
应用程序.ts
import dotenv from 'dotenv';
dotenv.config();
import { PubSub } from '@google-cloud/pubsub';
import express from 'express';
import PubSubConnection from "./PubSubConnection";
import { sendTestMessage } from './listenSub';
const app = express();
// DI (dependency injection)
// Create a one time pubsub as you will certainly only use the same project_id across your app
const pubSub = new PubSub({ projectId: process.env.PROJECT_ID });
// Create the connection to the topic/subscription dynamically here or anywhere else
// in your di where your service/component need an access to a special topic/subscription
const pubSubConnection = new PubSubConnection(pubSub, 'your_topic_name', 'your_sub_name');
// this members could be passed to any services that your app runs
const topic = pubSubConnection.getTopic;
const subscription = pubSubConnection.getSubscription;
// And use as many function anywhere you need it
// by just passing it the already created topic/subscription
sendTestMessage(topic);
app.listen(process.env.PORT, () => {
console.log('Server started');
});
PubSubConnection.ts
// Dotenv should always be loaded first in your application (aka app.js in your case) and in the 1st line
// because it only need to be loaded 1 time for the env variable to be accessible
// there is no neeed to load it everywhere you need to access env var
// require('dotenv').config();
import { PubSub, Subscription, Topic } from '@google-cloud/pubsub';
export default class TopicSubConnection {
topic: Topic
subscription: Subscription
constructor(
private readonly pubSub: PubSub,
private readonly topicName: string,
private readonly subscriptionName: string
) {
this.topic = new Topic(this.pubSub, this.topicName);
this.subscription = new Subscription(this.pubSub, this.subscriptionName);
}
public get getTopic(): Topic {
return this.topic;
}
public get getSubscription(): Subscription {
return this.subscription;
}
}
子操作.ts
import { Subscription, Topic, Message } from "@google-cloud/pubsub";
let messageCount = 0;
export const listenForMessages = (subscription: Subscription): void => {
const messageHandler = (message: Message) => {
console.log(`Received message ${message.id}:`);
console.log(`\tAgresso Data: ${message.data}`);
//console.log(`\tAttributes: ${message.attributes}`);
messageCount += 1;
message.ack();
};
// the handler could be an array of functions, this means you could have different
// functions listening to the same subscription
subscription.on('message', messageHandler);
};
export const sendTestMessage = (topic: Topic): void => {
let messageObject = {
action: 'insert',
objectType: 'resource',
objectId: 12456,
stamp: Date.now()
}
topic.publish(Buffer.from(JSON.stringify(messageObject)));
console.log('sent test message')
};
推荐阅读
- breadcrumbs - 面包屑在 Google 搜索结果中不可点击
- parameters - Blazor 级联参数
- c++ - 尝试删除动态数组时遇到的困难
- python - 如何将列表重新格式化为列表列表?
- terraform - 使用 Terraform 将快照还原到 GCP 卷
- java - 集成测试 - 虚拟 SSH 服务器未正确执行 ssh 命令
- c# - $(TargetFrameworkVersion) (v9.0) 小于 Xamarin.Forms (10.0) 所需的最低 $(TargetFrameworkVersion)
- python - 正则表达式前瞻和后视
- python-3.x - 如何在没有http请求的情况下更改python代码中的ip或代理
- mysql - 如何编写一个mysql查询来解决这个问题?