[英]Real time notifications node.js
我正在使用Node.js,express.js和Sequelize開發日歷應用程序。
該應用程序很簡單,您可以在日歷中創建任務,但也可以將一些任務分配給系統的其他用戶
我需要使用socket.io創建一個通知系統,但是我沒有使用websockets的經驗。 我最大的疑問是如何使服務器將通知發送給您您分配任務的用戶?
我的端口配置位於名為bin / www的文件夾中,我的快捷路由位於名為server.js的文件中
任何想法?
我想向您介紹隨時可用的后端系統,該系統使您能夠輕松構建具有出色功能的現代Web應用程序:
這樣,您可以專注於主要的應用程序開發。
您可以看一下Kuzzle ,這是我正在從事的一個項目:
首先,啟動服務: http : //docs.kuzzle.io/guide/getting-started/#running-kuzzle-automagically
然后在您的日歷應用程序中,您可以使用javascript sdk
此時,您可以創建一個文檔:
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)
我認為這可以幫助您:)
通過socket.io或本機Websocket提供文檔(如果可用)
不要猶豫,提問題;)
據我了解,您需要將socket.io實例傳遞給其他文件,對嗎?
var sio = require('socket.io');
var io = sio();
app.io = io;
您只需將其附加到bin / www文件中的服務器上
var io = app.io
io.attach(server);
或者我還想做的是添加用於表達的socket.io中間件
// Socket.io middleware
app.use((req, res, next) => {
req.io = io;
next();
});
因此,您可以在某些路由器文件中訪問它
req.io.emit('newMsg', {
success: true
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.