0

I'm working on an iOS App with an socket.io/koa (https://github.com/koajs/koa) service connection. For testing the service I'm using thor (https://github.com/observing/thor). The problem is, that my socket.io service wont return anything. When I look at thors response, I see, that there is a connection, but no callback from the service. This is my code for building and testing the socket.io service:

var server = require('http').Server(app.callback()),
    io = require('socket.io')(server);

io.on('connection', function(socket) {
  socket.emit('news', { hello: 'world' });
  console.log('it works.');
});

In my opinion there should be a log on the console and the written "{ hello: 'world' }" in the view of my client. Is there a problem with koa, or am I doing anything wrong?

teawithfruit
  • 767
  • 1
  • 11
  • 22
  • 1
    what version of `socket.io`? what's your client code look like? I've always been able to tell if the socket.io bound to my server instance correctly by uses the client-side io library and trying to connect. That or you can look at your server connection log, the socket.io client-side library tries to connect on whatever you provide as the `path:` option when creating a client-side socket. Do you see any connection attempts at `/socketio` or the like? – jbielick Mar 01 '15 at 20:41

1 Answers1

0

If your cannot see "it works" on the terminal where you start your application, there was no socket or web-socket connected to your server application.

I wouldn't try to test Koa by using Thor before your application doesn't work. Koa is unstable.

Looking at your code I assume you want to do something like:

app.io.route('news', function* (next, data){
  console.log("Server terminal output for client emited event news: ",data);
  this.emit('ok',{news: 'received'});
  this.broadcast.emit('news',data);
});

Notice the asterisk after the function. This is what is the main difference instead of using Express.

The connection event is used on client site:

socket.on('connect', function () {
  socket.emit('news',{hello: 'world'});
  socket.on('ok',function(data){
    console.log('this message is written on browser console',data);
  });
})

On the client site there is no asterisk.

Danny
  • 1,078
  • 7
  • 22
  • 2
    Koa2 is stable and uses async/await. It is recommended [middleware](https://medium.com/@grantminer/why-you-should-use-koa-with-node-js-7c231a8174fa) instead express – Yones Jan 27 '19 at 01:11