0

Here is my app.js

redis.on('message', function(channel, message) {

    //console.log('Redis: Message on ' + channel + ' received!');
    //
    //message1 = JSON.parse(message).object;
    //console.log(message1);
    //console.log(message1.question_model_user_id);

    message = JSON.parse(message);
    io.emit('new', message);

});

console.log('user connected');

socket.on('disconnect', function(){
    console.log('user disconnected');
});

now my client script

var socket = io('http://localhost:3000');

socket.on('new',function(data){
    $.each( data, function( key, value ) {
        console.log(value);
      //$.each( value, function( key, value1 ) {
      //    $.each( value1, function( key, value3 ) {
      //        console.log(key);
      //    });
      //});
    });

Now user A have one object and user B has 0 object. Whenever A refreshes the page B should get one object but i am getting many objects as the same objects increases like +1 if user A refreshes the page only once.

3
  • A question would be welcome. What is redit on your server code ? We need context to understand what you mean. Commented Jul 11, 2015 at 17:32
  • Oh i am sending the object on the test-channel via redis subscribe so if the subscribe is made redis.on message would run user a has one object which has 8 array and b got 0 object now whenever a refresh b would get one object now next time if b refresh a gets empty object now if a refresh b get 2 objects? Commented Jul 11, 2015 at 17:35
  • issue has been resolved some one told me to io.emit to send objects to all the connected client and i used socket.emit now the objects are not getting +1 every time page is being refresh thanks everyone Commented Jul 11, 2015 at 17:51

1 Answer 1

1

Use io.socket instead of io.emit to emit the object oncee

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.