2014-12-31 2 views
2

pusher-client-node을 통해 소비되는 데이터 스트림을 Socket.IO를 사용하여 클라이언트로 보내려고합니다.Socket.io - 푸시 스트림에서 클라이언트로 데이터 보내기

이 같은 Node.js를 내 데이터를 수신하고 있습니다 : 지속적으로 제공

var API_KEY = 'cb65d0a7a72cd94adf1f'; 
var pusher = new Pusher(API_KEY, { 
    encrypted: true 
}); 
var channel = pusher.subscribe("ticker.160"); 
channel.bind("message", function (data) { 
    //console.log(data); 
}); 

내 데이터는 그 다음과 같습니다

내 Socket.IO 코드는 다음과 같습니다

{ channel: 'ticker.160', 
    trade: 
    { timestamp: 1420031543, 
    datetime: '2014-12-31 08:12:23 EST', 
    marketid: '160', 
    topsell: { price: '0.00007650', quantity: '106.26697381' }} 
:

/** 
* Socket.io 
*/ 
var io = require("socket.io").listen(server, {log: true}); 
var users = []; 

var stream = channel.bind("message", function (data) { 
    console.log(data); 
}); 

io.on("connection", function (socket) { 
    // The user it's added to the array if it doesn't exist 
    if(users.indexOf(socket.id) === -1) { 
     users.push(socket.id); 
    } 

    // Log 
    logConnectedUsers(); 

    socket.emit('someevent', { attr: 'value' }) 

    stream.on("newdata", function(data) { 
     // only broadcast when users are online 
     if(users.length > 0) { 
      // This emits the signal to the user that started 
      // the stream 
      socket.emit('someevent', { attr: 'value' }) 

     } 
     else { 
      // If there are no users connected we destroy the stream. 
      // Why would we keep it running for nobody? 
      stream.destroy(); 
      stream = null; 
     } 
    }); 

    // This handles when a user is disconnected 
    socket.on("disconnect", function(o) { 
     // find the user in the array 
     var index = users.indexOf(socket.id); 
     if(index != -1) { 
      // Eliminates the user from the array 
      users.splice(index, 1); 
     } 
     logConnectedUsers(); 
    }); 
}); 

// A log function for debugging purposes 
function logConnectedUsers() { 
    console.log("============= CONNECTED USERS =============="); 
    console.log("== :: " + users.length); 
    console.log("============================================"); 
} 

나는 Node.JS. 내 푸셔 스트림을 사용하기 Node.js를하고 Socket.IO와 투쟁에 아주 새로운 오전 따라서 내 질문 : 어떻게 내 Pusher 코드 내 Socket.IO 코드를 연결하려면? 당신은 socket/io rooms를 사용할 필요가

+1

당신이 (참조 푸셔 노드 라이브러리 https://www.npmjs.com/package/pusher)는 Pusher에게 정보를 게시하기위한 것이며 데이터를 소비하는 것이 아닙니다. "다음과 같은 노드에 데이터를 수신하고 있습니다."다음에 제공 한 코드는 참조한 라이브러리에 유효하지 않습니다. 이 코드는 Pusher JS 라이브러리를 사용할 때 웹 브라우저에서만 작동합니다. https://github.com/pusher/pusher-js – leggetter

+0

@leggetter Thx! 그러나 socket.io 연결을 시도 할 때와 동일한 nodeJS 인스턴스에서 푸셔 스크립트를 실행 중이고 데이터를 지속적으로 수신합니다 ... – mrquad

+1

질문에서 참조한 Node.js 라이브러리 (https : //www.npmjs. co.kr/package/pusher)가 잘못 되었습니까? 다음 라이브러리는 질문에 표시되는 API를 제공합니다. https://github.com/dirkbonhomme/pusher-client-node – leggetter

답변

1

...

서버 :

var channel = pusher.subscribe("ticker.160"); //subscribe to pusher 

//pass messages from pusher to the matching room in socket.io 
channel.bind("message", function (data) { 
    io.to('ticker.160').emit('room-message', {room:'ticker.160', data:data}); 
}); 

... 

io.on("connection", function (socket) { 
    ... 
    socket.on('join', function(room){ 
    socket.join(room); 
    }); 
    socket.on('leave', function(room){ 
    socket.leave(room); 
    }); 
}); 

클라이언트 :

io.emit('join','ticker.160'); 
io.on('room-message', function(message){ 
    switch(message.room) { 
    case 'ticker.160': return doSomething(message.data); 
    ... 
    } 
}); 
+0

질문에 대한 나의 의견대로 'var channel = pusher.subscribe ("ticker.160")'는 질문에서 참조 된 Pusher Node.js 라이브러리에는 유효하지 않습니다. 이 라이브러리는 '푸셔 뺄셈 (pusher.subscribe)'기능을 제공하지 않습니다. 이 코드는 클라이언트 측 코드와 훨씬 비슷하게 보입니다. – leggetter

+0

@leggetter는 언급을 주셔서 감사합니다. 맨 아래쪽에 메모를 추가했습니다. 요즘에는 node.js 지원과 함께 npm에 게시되지 않은 JS 라이브러리를 보니 수치 스럽습니다. – Tracker1

+0

@mrquad는 https://github.com/dirkbonhomme/pusher-client-node가 실제로 사용되고 클라이언트 기능을 제공한다고 명확히했습니다. 채널을 구독하고 서버에서 이벤트를 수신하는 기능. 그래서, 당신은 대답이 맞는 것처럼 보입니다. – leggetter