简体   繁体   English

实时通知node.js

[英]Real time notifications node.js

I'm developing a calendar application with Node.js, express.js and Sequelize. 我正在使用Node.js,express.js和Sequelize开发日历应用程序。

The application is simple, you can create tasks in your calendar, but you can also assign some tasks to others users of the system 该应用程序很简单,您可以在日历中创建任务,但也可以将一些任务分配给系统的其他用户

I need to create a notification system with socket.io, but I'm have no experience with websockets. 我需要使用socket.io创建一个通知系统,但是我没有使用websockets的经验。 My big doubt is how can I make that my server sends a notification to the user that you assign the task? 我最大的疑问是如何使服务器将通知发送给您您分配任务的用户?

My ports configurations is on a folder called bin/www, my express routes are difined on a file called server.js 我的端口配置位于名为bin / www的文件夹中,我的快捷路由位于名为server.js的文件中

Any Idea? 任何想法?

I want to introduce you to ready to use backend system that enables you to easily build modern web applications with cool functionalities: 我想向您介绍随时可用的后端系统,该系统使您能够轻松构建具有出色功能的现代Web应用程序:

  • Persisted data: store your data and perform advanced searches on it. 永久数据:存储数据并对其进行高级搜索。
  • Real-time notifications: subscribe to fine-grained subsets of data. 实时通知:订阅细粒度的数据子集。
  • User Management: login, logout and security rules are no more a burden. 用户管理:登录,注销和安全规则不再是负担。

With this, you can focus to your main application development. 这样,您可以专注于主要的应用程序开发。

You can look at Kuzzle , wich is one project I working on: 您可以看一下Kuzzle ,这是我正在从事的一个项目:

First, start the service: http://docs.kuzzle.io/guide/getting-started/#running-kuzzle-automagically 首先,启动服务: http : //docs.kuzzle.io/guide/getting-started/#running-kuzzle-automagically

Then in your calendar application you can the javascript sdk 然后在您的日历应用程序中,您可以使用javascript sdk

At this point you can create a document: 此时,您可以创建一个文档:

const 
  Kuzzle = require('kuzzle-sdk'),
  kuzzle = new Kuzzle('http://localhost:7512');


const filter = {
    equals: {
        user: 'username'
    }
}


// Subscribe every changes in calendar collection containing a field `user` equals to `username`
kuzzle
    .collection('calendar', 'myproject')
    .subscribe(filter, function(error, result) {
        // triggered each time a document is updated/created !
        // Here you can display a message in your application for instance
        console.log('message received from kuzzle:', result)
    })


// Each time you have to create a new task in your calendar, you can create a document that represent your task and persist it with kuzzle
const task = {
    date: '2017-07-19T16:07:21.520Z',
    title: 'my new task',
    user: 'username'
}

// Creating a document from another app will notify all subscribers
kuzzle
    .collection('calendar', 'myproject')
    .createDocument(task)

I think this can help you :) 我认为这可以帮助您:)

Documents are served though socket.io or native websockets when available 通过socket.io或本机Websocket提供文档(如果可用)

Don't hesitate to ask question ;) 不要犹豫,提问题;)

As far as I can understand you need to pass your socket.io instance to other files, right ? 据我了解,您需要将socket.io实例传递给其他文件,对吗?

var sio = require('socket.io');

var io = sio();
app.io = io;

And you simply attach it to your server in your bin/www file 您只需将其附加到bin / www文件中的服务器上

var io = app.io
io.attach(server);

Or what else I like to do, is adding socket.io middleware for express 或者我还想做的是添加用于表达的socket.io中间件

// Socket.io middleware
app.use((req, res, next) => {
    req.io = io;
    next();
});

So you can access it in some of your router files 因此,您可以在某些路由器文件中访问它

req.io.emit('newMsg', {
    success: true
});

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

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