Carol.Kar
Carol.Kar

Reputation: 5345

Socket.io - Sending data from a pusher stream to the client

I am struggling to send a stream of data being consumed via pusher-client-node to the client using Socket.IO.

I am receiving my data in Node.JS like this:

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);
});

My data, which comes in continuously, looks like that:

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

My Socket.IO code looks like this:

/**
 * 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("============================================");
}

I am quite new to Node.JS and Socket.IO and struggle to use my pusher stream in Node.JS. Therefore my question: How to connect my Socket.IO code with my Pusher code?

Upvotes: 2

Views: 2321

Answers (1)

Tracker1
Tracker1

Reputation: 19334

You need to use socket/io rooms ...

server:

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);
  });
});

client:

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

Upvotes: 2

Related Questions