using koa and socket.io to serve static pages and chat messages

mfc

I am using koa and socket.io to serve both static html pages and process chat messages sent to the socket. The following code works as a server :-

var app = require('koa')();
var router = require('koa-router')();
var views = require('co-views');
var render = views('.', { map: { html: 'swig' }});
                                                              <--- [1]
router.get('/', function *(next) {
    console.log('index.html');
    this.body = yield render('index.html');
});

app.use(router.routes());

var server = require('http').createServer(app.callback());    <--- [2]
var io = require('socket.io')(server);                        <--- [3]

io.on('connection', function(socket) {
    socket.on('chat message', function(msg, callback) {
        console.log('got message - ' + msg);
        io.emit('chat message', msg);
        if (callback) {
            callback();
        }
    });
});

server.listen(8080);

my question is; if I move [2] and [3] up to [1] with the rest of the server set up the get router doesn't work.

Does anyone know why?

The following is the client code :-

var io = require('socket.io-client');
var socket = io('http://localhost:8080');

socket.on('connect', function() {
    console.log('socket connected');

    socket.emit('chat message', 'hello world !!!!!!', function() {
        console.log('emit');
        socket.close();
    });
});

socket.on('disconnect', function() {
    console.log('socket disconnect');
});
Peadar Doyle

When you call app.callback() a callback function for handling HTTP requests is created. Based on the Koa source code it looks like the middleware stack is loaded into the callback when it is created. As such after the callback is created any further middleware added to the stack via the use() method will not be used.

Following the same logic; if you were using the Koa listen() method it would need to be called after all uses of the Koa use() method. Otherwise these middlewares would not actually be on the middleware stack that the server is using.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Chat App Using React and Socket.io Hanging After Sending Too Many Messages

How to serve static pages

private chat using socket.id on socket.io

ionic chat application using socket.io(how to make my user to join chat.socket.io)

How to disconnect all sockets serve side using socket.io?

Struggling with typescript in chat application using socket.io

Create VOIP chat application using Socket.IO

Private chat using node.js and socket.io

private chat with socket.io

How to build and chat app (with Group Chat and Private Messaging )using node.js socket.io and express?

messages not getting added when using socket.io

some messages not received using nodeJS, mqtt and socket.io

Not receiving messages between two IOS devices using Socket.io

Why can't I serve static files from a Koa router?

How to serve static html pages with Rails?

Nodejs, express & socket.io communicate between two static client pages

Implementing Audio chat with Socket.IO and NodeJS

Socket IO Node Angular Chat Service

Socket.io Chat Tutorial not functioning properly

Chat project - load balance with socket.io

Socket.io in laravel 5 for chat

Socket.IO emitting chat message to room

How to do One on One chat in Socket.io using Node JS server?

Client-Server architecture behind chat applications using socket.io

How to check how many participants are connected to my chat room using socket IO

tyrning to create a chat sever using socket.io and I am getting an error I dont understand

Koa-mount/router + socket.io integration

Can I have koa-static serve assets at a custom (e.g., /static/) path?

prevent clients sending messages socket.io