Kim Oliveros
Kim Oliveros

Reputation: 711

Socket.io emitting from reciever's browser but does not emit on the sender's browser

Hi I'm kinda new to socket.io so not really sure if what I'm doing is the proper/standard way but since its running fine I guess its fine. This app is chat application the script I worked on is able to store messages on database and loads them to the receiver and sender's browser... The problem is IT DOES NOT load on the sender's page unless I refresh the page whats more I find it weird because the receiver receives the data just fine without refreshing the page so I was like "what gives?"

here is the server.js

var Sequelize = require('sequelize'),
  app = require('express')(),
  http = require('http').Server(app),
  io = require('socket.io')(http);

var validator;
var messages = [];

var sequelize = new Sequelize('schat', 'root', '');

app.use('/assets', require('express').static(__dirname + '/assets'));
app.use('/temp', require('express').static(__dirname + '/temp'));
app.get('/', function(req, res){
  res.send(validator);
});

io.on('connection', function(socket){
  // Send all previously sent messages
  for( i in messages ) {
    socket.emit('chat message', messages[i]);
  }

  socket.on('chat message', function(msg){
    console.log('message: ' + msg);

    // Push the message into the in-memory array.
    messages.push(msg);

    // Storage the message for when the application is restarted.
    sequelize.query('INSERT INTO chat_storage(chat) VALUES("'+msg+'")').success(function() {
      // Insert was successful.
    }).error(function (err) {
      // Error inserting message
    });

    // Send the message to everyone
    socket.broadcast.emit('chat message', msg);
  });
});

function getStdout(command, args, fn) {
  var childProcess = require('child_process').spawn(command, args);
  var output = '';
  childProcess.stdout.setEncoding('utf8');
  childProcess.stdout.on('data', function(data) {
    output += data;
  });
  childProcess.on('close', function() {
    fn(output);
  });
}

// Load Messages
sequelize.query('SELECT chat FROM chat_storage ').success(function (rows)  {
  for( i in rows ) {
    messages.push(rows[i].chat);

  }
  getStdout('php', ['message.php'], function(output) {
    validator = output;
    http.listen(3000, function(){
      // Start server.
    });
  });
}).error(function (err) {
  // Error!
});

Then this is the page that loads the chat

<?php startblock('script') ?>

 <script src="/socket.io/socket.io.js"></script>
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script>

 var socket = io();
  $('form').submit(function(){
    socket.emit('chat message', $('#m').val());
    $('#m').val('');
    return false;
  });
  socket.on('chat message', function(msg){
    $('#messages').append($('<li class="bubble">').text(msg));
  });


    </script>


  <?php endblock(); ?>

Any suggestion or am I missing something?? I tried changing socket.broadcast.emit('chat message', msg); to socket.emit('chat message', msg); but it does not work it does do load on the sender's browser but not on the reciever please help me

Upvotes: 0

Views: 431

Answers (1)

FunnyLookinHat
FunnyLookinHat

Reputation: 579

:)

Either add the message locally in the client with this line:

$('#messages').append($('li').text($('#m').val()));

Or replace the following:

socket.broadcast.emit('chat message', msg);

With this:

io.sockets.emit('chat message', msg);

Upvotes: 1

Related Questions