Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Problem with socket.emit from elephant that does not send properly #68

Closed
ToToSe opened this issue Aug 29, 2014 · 21 comments
Closed

Problem with socket.emit from elephant that does not send properly #68

ToToSe opened this issue Aug 29, 2014 · 21 comments

Comments

@ToToSe
Copy link

ToToSe commented Aug 29, 2014

x

@sroze
Copy link

sroze commented Aug 29, 2014

Is there any error ?

On Friday, August 29, 2014, ToToSe notifications@github.com wrote:

Hello,
I use elephant.io with socket.io for my tchat to send new message,
but when i'm sending messages they do not pass all.

Here it is my php code for sending sockets :

$socket = new Client(new Version1X('http://ourIpServer:8080'));
$socket->initialize();
$socket->emit('_ServerNewMsg', ['username' => $username, 'message' =>
$message, 'token' => 'xxx']);
$socket->close();

Thx for the help (and sorry for my bad english ! :) )


Reply to this email directly or view it on GitHub
#68.

@ToToSe
Copy link
Author

ToToSe commented Aug 29, 2014

No we've got 0 error.
(I use try)
try
{
$socket = new Client(new Version1X('http://127.0.0.1:8080'));
$socket->initialize();
$socket->emit('_ServerNewMsg', ['username' => $username, 'message' => $message, 'token' => 'xxx']);
$socket->close();
}
catch(Exception $e) {
exit($e);
}
echo 'good!';

it allways show good


EDIT : We used winston to try to find where the problem came from and when we send a socket, when it work it show this on nodeJs :

2014-08-29T16:55:34.958Z - info: SocketIO > Connected socket Zi6NAyAZGeO0aiLrAAAQ
{ message : 'test' }
2014-08-29T16:55:34.960Z - info: SocketIO > Disconnected socket Zi6NAyAZGeO0aiLrAAAQ

But when the socket doen't send it show this :
2014-08-29T16:55:34.958Z - info: SocketIO > Connected socket Zi6NAyAZGeO0aiLrAAAQ
2014-08-29T16:55:34.960Z - info: SocketIO > Disconnected socket Zi6NAyAZGeO0aiLrAAAQ

As you can see, Elephant.io is able to connect to our server, but the socket doens't emit :/


@kbu1564
Copy link
Contributor

kbu1564 commented Aug 29, 2014

@ToToSe I'm so sorry... I don't understand to your think.

Did you send ['username' => $username, 'message' => $message, 'token' => 'xxx'] from Client::emit() function on elephant.io?

Did you show to debug message of emit data like before is { message : 'test' }?

@kbu1564
Copy link
Contributor

kbu1564 commented Aug 29, 2014

Maybe the version about socket.io of nodejs would be 0.X.
Can you test for code like next?

$socket = new Client(new Version0X('http://ourIpServer:8080'));
$socket->initialize();
$socket->emit('_ServerNewMsg', ['username' => $username, 
                                'message' => $message,
                                'token' => 'xxx']);
$socket->close();

@ToToSe
Copy link
Author

ToToSe commented Aug 29, 2014

Still doens't work :/, and yes i send ['username' => $username, 'message' => $message, 'token' => 'xxx'] with Client::emit() function on elephant.io

@kbu1564
Copy link
Contributor

kbu1564 commented Aug 30, 2014

@ToToSe Can you show to me the source about socket.io of nodejs?

If the version of socket.io was used 1.0.0 more, execute of console command like next.

anonymous@localhost your socket.io path $ DEBUG=socket.io* node your-server

and
Can you show to output string(debug messages) to me?

@Taluu Taluu added 3.0 labels Aug 30, 2014
@Taluu
Copy link
Contributor

Taluu commented Aug 30, 2014

Could you just paste here your node script that receives this event (just the socket.on(...), even if it is only this call that I'm interested in.

The thing is, I also had this problem yesterday, but after a while, it seemed to work by itself when I did nothing, so I'm not sure if it is not a problem from socket.io or indeed one from Elephant.

Marking this ticket as "support" until a bug is found, if there's one.

@ve3
Copy link

ve3 commented Sep 1, 2014

Me too, some time does not send properly.
There is no error.
From the log it is just connected and then disconnected.

$client = new \ElephantIO\Client(new \ElephantIO\Engine\SocketIO\Version1X('http://localhost:1337'));
$client->initialize();
$emit_result = $client->emit('broadcast', array('sayhi' => 'Hello Hi :: ' .date('Y-m-d H:i:s')));
$client->close();
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var port       = 1337;
var fs = require('fs');
var logger     = require('winston');

function handler (req, res) {
    fs.readFile(__dirname + '/index.html', function (err, data) {
        if (err) {
            res.writeHead(500);
            return res.end('Error loading index.html');
        }

        res.writeHead(200);
        res.end(data);
    });
}

// Logger config
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, { colorize: true, timestamp: true });
logger.info('SocketIO > listening on port ' + port);

// listen to what port?
app.listen(port, function(){
    //console.log('listening on *:'+port);
    logger.info('Listening on *:'+port);
});

/*app.get('/', function(req, res){
    res.sendfile('index.html');
});*/ // no need, use handler instead.

io.on('connection', function(socket){
    //console.log('a user connected : '+socket.id);
    logger.info('SocketIO > Connected socket '+socket.id);

    socket.on('broadcast', function (message) {
        //console.log('ElephantIO broadcast > ' + JSON.stringify(message));
        logger.info('ElephantIO broadcast > ' + JSON.stringify(message));

        socket.broadcast.emit('broadcast', message);
    });

    socket.on('disconnect', function () {
        //console.log('SocketIO > Disconnected socket ' + socket.id);
        logger.info('SocketIO > Disconnected socket ' + socket.id);
    });
});

Test by reload php page every second. Reload 10 times but data send about 6-9 times.
I'm not sure is this elephant.io problem or socket.io problem.

@Taluu
Copy link
Contributor

Taluu commented Sep 2, 2014

@kbu1564 wrote in #71

I was catched exception about this problem. This problem of @ve3 will occur to source like next.

$client = new Client(new Version1X('http://localhost:1337'));
$client->initialize();
for ($i = 0; $i < 10; $i++) {
    $emit_result = $client->emit('broadcast', ['sayhi' => 'Hello Hi :: ' .date('Y-m-d H:i:s'),
                                               'index' => $i]);
}
$client->close();

When the code of before is testing, Send to 10 times but data send about 6-9 times.
@Taluu I think that this problem should check about version 1.x and 0.x of elephant.io.

execute code about before:
pc2

The Results of all test cases was executed about code to same.

@Taluu
Copy link
Contributor

Taluu commented Sep 2, 2014

So i'll test it with 2.0 and see if this is a problem of either the payload or something else

@Taluu Taluu added the bug label Sep 2, 2014
@Taluu
Copy link
Contributor

Taluu commented Sep 2, 2014

It seems I can't make the legacy example work (an offset not set on the transport), but I think I could reproduce this, but I have no clue as to why it doesn't send all the messages.

Sometimes I have all the messages sent, sometimes I don't (for 10 messages sent, I am receiving between 2 to 7 messages) ; even adding a small sleep time via a usleep doesn't seem to do the trick... I'm gonna check if the messages are indeed sent (it the call to fwrite is done).

@Taluu
Copy link
Contributor

Taluu commented Sep 8, 2014

OK, back from some more tests ;

It would seem that it indeed comes from a problem within Elephant 3, as the legacy seems to work pretty well. I also tried the Version0x and Version1x with elephant 3, which revealed that both of those were having problems.

I added in the write method another sleep time usleep(10000) (10 milliseconds), which seems to make everything work. I think I should now ponder which time is the best, so that it doesn't cause any other error (would it be due to the socket being too overloaded between its message, hence making it miss some of those ?), but not something too far as it would kind of ruin the interest of using socket.io.

Here is the gist that show how I debugged everything : https://gist.github.com/Taluu/21c30eedabc151d08fc0

@kbu1564 & @ve3 : could you try to add, just at the end of the write method (after the call to fwrite) the said usleep(10000);, and see if the results are acceptable ? Thanks.

@ve3
Copy link

ve3 commented Sep 8, 2014

Yes, it works very well.

@guillaumepotier
Copy link
Member

I recall we experienced this behavior in 2.x version, and we had to ensure transport went well by adding usleep too. We made many tests and found that 100ms was the "smallest safest" acceptable value. We should stick to that again by default, with maybe a way to customize it by config.

@Taluu
Copy link
Contributor

Taluu commented Sep 8, 2014

On my tests, it seems that 4ms is enough, but meh, I'll stick to the 100ms then (one never knows).

I'm adding a config for that while I'm at it.

@sroze
Copy link

sroze commented Sep 8, 2014

Just as external point of view, it's strange to have to hack like that (sleeping a certain amount of ms). Do you know how the JS socket.io client is handling that behaviour ?

@Taluu
Copy link
Contributor

Taluu commented Sep 8, 2014

It doesn't seem so (as far as I can decode node scripts...), but I also think that such a behaviour is strange indeed.

But as I have nothing else, and this seems to work, it should work as a temporary patch until someone can gives us more insight on that.

@guillaumepotier
Copy link
Member

We might avoid this hack by implementing the callback return to acknowledge the message has successfully been sent before trying to send another one. No ? cc @Taluu

@sroze
Copy link

sroze commented Sep 8, 2014

It's looking like a best design fix this issue :) BTW, this may cause performance regressions... Maybe we could implement an "sending buffer" which is waiting acks of a certain number of messages, instead of just one?

@Taluu
Copy link
Contributor

Taluu commented Sep 8, 2014

I tried to check if there was a return (by reading the socket), but it doesn't seem so, or it is gibberish.. Seems to be some leftovers from the socket.

If you feel like finding a better solution, feel free to implement it ! Right now, this rudimentary patch should do the trick.

@Taluu Taluu closed this as completed in 61f7eb8 Sep 8, 2014
@Taluu
Copy link
Contributor

Taluu commented Sep 8, 2014

OK I marked this ticket as fixed ; as I said, if there is a better solution, feel free to open a PR.

@Taluu Taluu added done and removed support labels Sep 8, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants