Angularjs 使用Socket.io Redis服务器正确发送通知
目标:将帖子添加到特定频道时,向该频道的所有已连接订阅用户发送通知 视图上的关系数据库表:Post、通道、用户、通道\用户Angularjs 使用Socket.io Redis服务器正确发送通知,angularjs,node.js,sockets,redis,socket.io,Angularjs,Node.js,Sockets,Redis,Socket.io,目标:将帖子添加到特定频道时,向该频道的所有已连接订阅用户发送通知 视图上的关系数据库表:Post、通道、用户、通道\用户 帖子:id、标题、内容、频道\ id(指出帖子与哪个频道相关) 频道:id、名称(频道列表) 用户:id、用户名(用户表) 频道用户:id、频道id、用户id(此表指示用户订阅的频道。) 目标:创建帖子时,向特定用户组发送通知 我已经尝试了几种方法,它正在发挥作用。但我正在寻找正确的方法 服务器端:socket.js 在创建新帖子时将数据发布到Redis服务器 $da
- 帖子:id、标题、内容、频道\ id(指出帖子与哪个频道相关)
- 频道:id、名称(频道列表)
- 用户:id、用户名(用户表)
- 频道用户:id、频道id、用户id(此表指示用户订阅的频道。)
$data = [
'event' => 'newPost',
'data' => [
'title' => $postTitle,
'content' => $postContent
]
];
$redisChannel = "channel-" . $channelId; // As per our naming conventions
Redis::publish($redisChannel, json_encode($data));
用户正在正确获得他们已订阅的频道的通知
问题1:我不确定这是实现此通知的最佳解决方案
问题2:当用户在多个浏览器选项卡中打开同一应用程序时,会收到所有这些选项卡的通知。它应该只发送一个通知。这与redis端服务器端的用户管理有关。这部分我也不太清楚
我非常感谢你在这方面的建议/帮助。先谢谢你
var app = require('express')();
var request = require('request');
var http = require('http').Server(app);
var io = require('socket.io').listen(http);
var Redis = require('ioredis');
var redis = new Redis(####, 'localhost');
// Get all channels from Database and loop through it
// subscribe to redis channel
// initialize namespace inorder to send message to it
for(var i=1; i=<50; i++) {
redis.subscribe('channel-'+i);
io.of('/namespace-channel-'+i).on('connection', function(socket){
console.log('user connected');
});
}
// We have been subscribed to redis channel so
// this block will hear if there is any new message from redis server
redis.on('message', function(channel, message) {
message = JSON.parse(message);
// send message to specific namespace, with event = newPost
io.of('/namespace-'+channel).emit('newPost', message.data);
});
//Listen
http.listen(3000, function(){
console.log('Listening on Port 3000');
});
// userChannels: user subscribed channels array
// Loop through all channel and hear for new Post and
// display notification if there is newPost event
userChannels.forEach(function (c) {
let socket = {};
console.info('Attempting to connect to namespace'+c);
socket = io.connect(SOCKET_URL + ':3000/namespace-'+c, {query: "Authorization=token"});
socket.on('connect',function(){
if(socket.connected){
console.info("namespace-" + c + " Connected!");
// On New post event for this name space
socket.on('newPost', function(data) {
/* DISPLAY DESKTOP NOTIFICATION */
});
}
});
});
$data = [
'event' => 'newPost',
'data' => [
'title' => $postTitle,
'content' => $postContent
]
];
$redisChannel = "channel-" . $channelId; // As per our naming conventions
Redis::publish($redisChannel, json_encode($data));