【问题标题】:Express Application Instant Notification Architecture SuggestionsExpress 应用即时通知架构建议
【发布时间】:2017-06-22 02:58:10
【问题描述】:

我有一个快速应用程序,当新用户注册到数据库时,管理员应该会收到即时通知。请注意,我使用 Passport 通过 Google 策略登录用户:

  passport.use(new GoogleStrategy({
    clientID    : configAuth.googleAuth.clientID,
    clientSecret: configAuth.googleAuth.clientSecret,
    callbackURL : configAuth.googleAuth.callbackURL,
  },
  function(token, refreshToken, profile, done){

    process.nextTick(function(){
      // try to find a user based on their google id
      User.findOne({ 'google.id' : profile.id }, function(err, user){
        if(err)
          return done(err)

        if(user){
          // if a user is found, log them in
          return done(null, user)
        } else {

          // if the user isn't in our database, create a new user
          var newUser = new User({
            'google.id': profile.id,
            'google.token': token,
            'google.name': profile.displayName,
            'google.email': profile.emails[0].value,  //pull the first email
            'google.active': 0,
            'google.level': 0
          })

          // save the user
          newUser.save(function(err){
            if(err)
              throw err
            return done(null, newUser)
          })

          // Give the new user the role of 'inactive'
          acl.addUserRoles(newUser.id, 'inactive', function(err){})

          // Email a notice to the new user that they have to be accepted by an administrator.
          ...

          // Send an email to all "admin-level" users to activate this user.
          acl.roleUsers('admin', function(err, users){
            // Loop through all admin users
            users.forEach(function(value){
              // Find each individual admin user by _id
              User.findOne({ '_id' : value }, function(err, user){
                if(err)
                  return done(err)

                // Send this admin an internal mail (IMail)
                var iMail = new IMail()
                iMail.body = newUser.google.name + ' needs approval.',

                // Send each administrator a notification that a new user needs approval
                iMail.recipients.push(user.id)
                iMail.save(function(err){
                  if(err)
                    return err
                  console.log("IMail was sent to notify admin " + user.google.name + " to approve " + newUser.google.name + " as a system user.")
                })
              })
            })
          })
        }
      })
    })
  }))

iMail.save(function(err){
      ...
})

块,我知道需要触发某种类型的事件,这会以某种方式提醒当前登录的每个管理员。不幸的是,这就是我的理解在这一点上崩溃的地方。我如何立即将此新通知通知管理员?我希望它像 Gmail,例如,当发送新电子邮件时收件箱自动显示 (1)。

作为旁注,我已经阅读了发布者/订阅者模式 (https://joshbedo.github.io/JS-Design-Patterns/) 并认为这可能会让我走上正轨,我只是不确定如何实现它。有人向正确的方向推动一点点将不胜感激。

【问题讨论】:

    标签: javascript node.js design-patterns


    【解决方案1】:

    @Maximuf Kingzy 是对的,您需要向管理员用户发送通知,并且由于您希望他们在在线时收到此通知,因此您可以使用套接字发送它们,但有时很难将其集成到 express 中。

    另一种选择:

    您可以将通知保存在数据库中并创建一个快速路由来检索这些通知。当管理员登录时,您只需检索这些通知。或者,如果您想要体验实时而不是实时,您可以使用 setInterval 每 X 秒检索一次这些通知,然后将它们显示给管理员用户。

    表达非常简单的路线(我的意思是非常简单):

    app.get('/imail', (req, res) => {
       Imail.find({}, (err, imails) => {
          if (err) {
             return res.status(400).end();
          }
          res.json(imails);
       });
    });
    

    在浏览器中:

    setInterval(() => {
      fetch('http://yourserver:serverport/imail').then((response) => {
        const contentType = response.headers.get("content-type");
        if (contentType && contentType.indexOf("application/json") !== -1) {
           return response.json().then(json => {
              return response.ok ? json : Promise.reject(json);
           });
        } else {
           return response.text().then(text => {
               return response.ok ? text : Promise.reject(text);
           });
        }
      }).then((imails) => {
        // do what you want with imails
      }).catch((err) => {
        // error
      });
    }, 15000);
    

    【讨论】:

    • 你有什么更好的框架(而不是 express)来使用套接字的建议吗?
    • express 可以使用套接字,就像所有其他框架一样,只是不喜欢将两者混合。我真的很喜欢拥有一个单独的 http REST 服务器和一个套接字服务器,但这只是个人喜好问题。
    猜你喜欢
    • 2020-01-29
    • 2012-08-04
    • 2012-04-10
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 2016-03-19
    • 2012-03-01
    相关资源
    最近更新 更多