简体   繁体   English

如何为网站撰写通知?

[英]How can I write notification for website?

I am building a website for myself. 我正在为自己建立一个网站。 I use MongoDB, feathers.js, Node.js and Angular 5. I would like to add Notification for it. 我使用MongoDB,feathers.js,Node.js和Angular 5.我想为它添加Notification。 It looks like notification's StackOverflow. 它看起来像通知的StackOverflow。 在此输入图像描述

I don't need push Notification. 我不需要推送通知。 Just want a red highlight exactly like notification's StackOverflow when I do some specific events. 当我做一些特定事件时,只想要一个与通知的StackOverflow完全相同的红色突出显示。 I am new so please give me details about what I need to write both front and back end or some good sources about it. 我是新手,所以请给我详细说明我需要写的前端和后端或者有关它的一些好消息。 Thank in advanced. 谢谢高级。

Create a service that requests notifications from the server side via HTTP. 创建一个通过HTTP从服务器端请求通知的服务。 Your component that should show that red dot will use that service and change its state depending on the response from that service. 应显示红点的组件将使用该服务并根据该服务的响应更改其状态。 Here is the example of working with HTTP: HTTP in Angular So, create a service that requests from server notifications. 以下是使用HTTP的示例: Angular中的HTTP因此,创建一个从服务器通知请求的服务。 Then, create a component that does the representation logic, utilizing the service you created (it is done with Dependency Injection like here ). 然后,利用您创建的服务创建一个执行表示逻辑的组件( 这里使用依赖注入完成)。

There are two approaches to achieve this. 有两种方法可以实现这一目标。

  • You can use polling to get the notification for a user. 您可以使用轮询来获取用户的通知。 In polling, the client makes a request after a specific interval, say 5 min, to the server, to ask whether there is any new notification or not. 在轮询中,客户端在特定时间间隔(例如5分钟)之后向服务器发出请求,询问是否有任何新通知。 Then with the type of response you can show/hide the notification symbol 然后使用响应类型,您可以显示/隐藏通知符号

  • The other approach is when you use socket connection between the server and and the client to establish the communication. 另一种方法是在服务器和客户端之间使用套接字连接来建立通信。 When ever a notification needs to be sent to the user, the server pushes the notification via socket connection rather than waiting for the client to make a request. 当需要向用户发送通知时,服务器通过套接字连接推送通知,而不是等待客户端发出请求。

Both the approaches has got its own pros and cons, polling is much faster to implement but is not a good practice when you have millions of customers, because you server resource gets occupied. 这两种方法都有自己的优点和缺点,实现轮询要快得多,但是当你有数百万客户时,这不是一个好的做法,因为你的服务器资源被占用了。 Also polling would have some delay sending the notification since it is client depended. 此外,轮询会有一些延迟发送通知,因为它是客户端所依赖的。

Advice : If you are starting to implement is for few thousand users or hobby project, then go for polling. 建议 :如果你开始实施是为了几千个用户或业余爱好项目,那么去投票。 Consider socket connections only when you have the urgent need to show notification with zero lag and your customer base is atleast few millions. 仅当您迫切需要显示零滞后通知且您的客户群至少为几百万时,才考虑套接字连接。

If you are using socketio with feathers then its easy to implement this. 如果你正在使用带有羽毛的袜子,那么它很容易实现。

Read about it in event channels . 活动频道中阅读相关内容。

The idea is to create a channel in your server to get a hold of the connected users via socket. 我们的想法是在您的服务器中创建一个通道,以通过套接字保持连接的用户。

app.on('connection', connection => {
   app.channel('anonymous').join(connection);
});

Then if a service method is called, inform the connections in the channel. 然后,如果调用服务方法,则通知通道中的连接。

app.service('notifications').publish('created', (data, context) => {
  return app.channel('anonymous');
});

Then in your client side you will get the published data by listening to the method event. 然后在您的客户端,您将通过监听方法事件获取已发布的数据。

app.service('notifications').on('created', notification => {
      console.log('notification created', notification);
   });

Then if you want a live example, try feathers chat 然后,如果你想要一个实例,尝试羽毛聊天

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM