내 프로젝트는 외부 API와 통신하기 위해 프록시 서버로 Nodejs로 작성됩니다.Node.js 및 서버 보낸 이벤트에 Redis 사용 방법 이해
API는 Redis (pub/sub)를 통해 제품 업데이트를 전송합니다. 프록시 서버는 메시지를 처리하여 SSE (Server Sent Events)를 통해 클라이언트에 보냅니다.
Redis와 SSE를 사용하는 저로서는 처음으로 튜토리얼을 온라인에서 찾는 것이 구현하기 쉽고 실제로했습니다.
// Client Side
var source = new EventSource('/redis'); // /redis is path to proxy server
source.addEventListener('items', handleItemsCallback, false);
source.addEventListener('users', handleUsersCallback, false);
source.addEventListener('customers', handleCustomersCallback, false);
// Function sample...
function handleItemsCallback (msg) {
// Do something with msg...
}
프록시 서버에서 내가 레디 스 메시지를 처리 할 수 /redis
라우팅과 컨트롤러를 만든 : 클라이언트 측에서
난 그냥 내가 그것으로 무언가를 최대한 빨리 업데이트를받을 같은 EventSource
및 생성 :
exports.redisUpdates = function (req, res) {
// Redis Authentication
var redisURL = url.parse(process.env.REDISCLOUD_URL);
var client = redis.createClient(redisURL.port, redisURL.hostname, {ignore_subscribe_messages: false});
client.auth(redisURL.auth.split(":")[1]);
// let request last as long as possible
req.socket.setTimeout(0);
// Subscribe to channels
client.subscribe('items', 'users', 'customers');
// Handle messages
client.on('message', function (channel, message) {
res.write('retry: 5000\n');
res.write('event: ' + channel + '\n');
res.write('data: ' + message + '\n\n');
res.flush(); // If I do not add this it doesn't push updates to the client (?)
});
//send headers for event-stream connection
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
res.write('\n');
};
은 여러 가지 오류가 발생할 잘 작동 개발 환경에서 로컬로 사용하지만, 생산에서 사용, 앱은 Heroku가 에서 호스팅되는및 Heroku 메트릭 여러 개 표시 H18, H12, H27 오류;
간혹 /redis
전화가 돌아 오는 상태 인 503
;
내가 왜 모든 자습서 res.flush()
을 언급하지 않고 내가 처음 일을 수 있도록 자신에 의해 그것을 발견, 제대로 이러한 서비스를 사용하고 있다면 내가 이해하고자하는 것은 ... 모든 공정에서