javascript 客户端上的 Firebase FCM publisher/subscriber
Firebase FCM publisher/subscriber on javascript client
我们正在尝试使用 Firebase 在我们的前端上实施一个 publisher/subscriber 模型。
我们的目标:尽快将消息从 JS 客户端发布者传递到 JS 客户端消费者——最好使用 WebSockets。
Firebase 文档主要关注 Android/iOS 设备,但并未很好地阐明服务器端和客户端 JS 解决方案之间的区别。
我的理解是,我们主要需要将设备与服务器上的主题相关联,但是,我不清楚我们是否可以 publish/consume 没有服务器交互的主题。
此示例演示订阅主题和处理传入消息 – 但是 – 是否也可以直接发布到主题?
After you have created a topic, either by subscribing client app instances to the topic on the client-side or via the server API, you can send messages to the topic. If this is your first time building send requests for FCM, see the guide to your server environment and FCM for important background and setup information.
(摘自https://firebase.google.com/docs/cloud-messaging/ios/topic-messaging)这里他们提到了一个事实,可以直接从客户端订阅主题?为何如此?从上面的文档和示例来看,这似乎不可能?
有一个发布到主题的 NodeJS 示例:
var topic = 'highScores';
var message = {
data: {
score: '850',
time: '2:45'
},
topic: topic
};
// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
发送这个客户端是否可行?
FCM 的设计期望消息从后端传递并在前端接收。不推荐其他任何东西。
您当然可以尝试使用 REST API 从您的前端发送消息,但是您将打开安全问题,因为您必须向客户端应用程序提供您的服务帐户详细信息。如果您像这样公开您的安全帐户凭据,任何人都可以使用它并随意向您的应用程序发送消息(可能更多,具体取决于授予该帐户的权限)。只是不要那样做 - 仅将您的服务帐户凭据保存在安全的后端。
FCM 文档中的示例均显示使用后端 SDK(主要是 Firebase Admin SDK)发送消息。坚持下去。
我们正在尝试使用 Firebase 在我们的前端上实施一个 publisher/subscriber 模型。
我们的目标:尽快将消息从 JS 客户端发布者传递到 JS 客户端消费者——最好使用 WebSockets。
Firebase 文档主要关注 Android/iOS 设备,但并未很好地阐明服务器端和客户端 JS 解决方案之间的区别。
我的理解是,我们主要需要将设备与服务器上的主题相关联,但是,我不清楚我们是否可以 publish/consume 没有服务器交互的主题。
此示例演示订阅主题和处理传入消息 – 但是 – 是否也可以直接发布到主题?
After you have created a topic, either by subscribing client app instances to the topic on the client-side or via the server API, you can send messages to the topic. If this is your first time building send requests for FCM, see the guide to your server environment and FCM for important background and setup information.
(摘自https://firebase.google.com/docs/cloud-messaging/ios/topic-messaging)这里他们提到了一个事实,可以直接从客户端订阅主题?为何如此?从上面的文档和示例来看,这似乎不可能?
有一个发布到主题的 NodeJS 示例:
var topic = 'highScores';
var message = {
data: {
score: '850',
time: '2:45'
},
topic: topic
};
// Send a message to devices subscribed to the provided topic.
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});
发送这个客户端是否可行?
FCM 的设计期望消息从后端传递并在前端接收。不推荐其他任何东西。
您当然可以尝试使用 REST API 从您的前端发送消息,但是您将打开安全问题,因为您必须向客户端应用程序提供您的服务帐户详细信息。如果您像这样公开您的安全帐户凭据,任何人都可以使用它并随意向您的应用程序发送消息(可能更多,具体取决于授予该帐户的权限)。只是不要那样做 - 仅将您的服务帐户凭据保存在安全的后端。
FCM 文档中的示例均显示使用后端 SDK(主要是 Firebase Admin SDK)发送消息。坚持下去。