2

Trying to use socket.io-client with react-native (ios for now), so far connection / receiving server side events from client seems to be working fine. However I can't seem to emit any events from the client?

Client

var socket = io("http://localhost:3000");
    socket.on('connect', function(){
        socket.on('ping', function(e) {
            console.log('Server emitted ping: ' + e);
            socket.emit('pong', 'hi server!');
        });
        socket.on('disconnect', function(){
            console.log("disconnect");
        });
    });

Server(Node.js)

var io = require('socket.io')(server);
io.on('connection', function (socket) {
  console.log('connected...');
  socket.on('pong', function (data) {
    console.log("Hmm?");
    console.log(data);
  });

  setTimeout(function() {
    console.log("Saying hello");
    socket.emit('ping', { message: 'Hello from server ' + Date.now() });

  }, 1000);
});

So from the server side, I see the logs

connected...
Saying hello

And in the client I see "Server emitted ping...", but the pong event doesn't seem to be doing anything? I tried catching all events on the server through solutions mentioned in StackOverflow, but it looked like no event was coming from the client. Any ideas?

Using latest RN version 0.31.

Also seeing this error when I first run the app in Xcode, could it be the reason?:

[warn][tid:main][RCTEventEmitter.m:52] Sending `websocketFailed` with no listeners registered.
tetutato
  • 638
  • 5
  • 16

1 Answers1

0

please try:

io.sockets.on('connection', function(socket) {
    ....
})
JFAP
  • 3,617
  • 1
  • 24
  • 25
  • same issue :/ I have a feeling this is a bug with how react native is wrapping the emitter code – tetutato Aug 22 '16 at 04:42
  • Let me try creating an RN app to quickly run this – JFAP Aug 22 '16 at 04:44
  • Okay I will create an issue in github and reference this thread. – tetutato Aug 22 '16 at 04:53
  • Just realized I don't actually need to emit stuff from client to server. I could do that with basic Ajax requests, just needed the server to client events :) – tetutato Aug 23 '16 at 18:46
  • You may want to utilize bidirectional communication through sockets but it's up to you. :) – JFAP Aug 23 '16 at 22:56
  • But would that apply even when I'm just trying to do something like updating a table on client side? At the moment I'm just emitting an "update" event from the POST server route where the table is updated. – tetutato Aug 23 '16 at 23:01