Koa: How to get Socket.io 2 to work with Koa 2?

Created on 15 Aug 2017  ·  10Comments  ·  Source: koajs/koa

How can I use socket.io with koa 2? I tried the following but does not work:

```
import Koa from 'koa'
import socket from 'socket.io'
import http from 'http'

const app = new Koa()

// Basic.
app.use(async ctx => {
  ctx.body = 'Hello World'
})

const server = http.createServer(app.callback())
const io = new socket(server)

io.on('connection', function(socket){
    console.log('a user connected')
})

server.listen(3000)

```

I don't see 'a user connected' on my terminal.

Any ideas?

Most helpful comment

@pengliheng
const app = new Koa()
var server = require('http').createServer(app.callback())
var io = require('socket.io')(server)
server.listen("your port", "your host")

Remeber to listen on "server" not "app"

All 10 comments

Compared your example to a similar setup I have. Only difference I spotted is that I do

const io = socket(server);

Maybe new is not supported by socket.io anymore. Can't remember. Or maybe the problem is on client side.

@ilkkao I have just tried that but still the same. Can you show us the entire code that you do?

correction, @lautiamkok i tested your code, it works. I think we should close this issue.

how can koa2 + https +socket.io
to working??
i am now doing it and have no idear

@pengliheng
const app = new Koa()
var server = require('http').createServer(app.callback())
var io = require('socket.io')(server)
server.listen("your port", "your host")

Remeber to listen on "server" not "app"

@hophuochoanggia
Thank you. Works perfect!

My server.js as follows:

const fs = require('fs')
const path = require('path')

const Koa = require('koa')
const app = new Koa()

const template = fs.readFileSync(path.resolve(__dirname, './index.html'))
app.use(async (ctx) => {
    ctx.type = 'html'
    ctx.body = template
})

const server = require('http').createServer(app.callback())
const io = require('socket.io')(server)

io.on('connection', function(socket){
    console.log('connected')
    socket.on('chat', function(msg){
        console.log(msg)
        io.emit('chat', msg + "222222");
    });
});

server.listen(3000, () => {
    console.log('Application is starting on port 3000')
})

and index.html is as follows:

<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>基于Socket.io的聊天室DEMO</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font: 13px Helvetica, Arial; }
        form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
        form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
        form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
        #messages { list-style-type: none; margin: 0; padding: 0; }
        #messages li { padding: 5px 10px; }
        #messages li:nth-child(odd) { background: #eee; }
    </style>
</head>
<body>

    <ul id="messages"></ul>
    <form action="">
        <input id="m" autocomplete="off" />
        <button id="b">Send</button>
    </form>

</body>
</html>
<script src="http://cdn.socket.io/socket.io-1.2.0.js"></script>
<script>
    const socket = io('http://localhost:3000')
    const text = document.querySelector('#m')
    const button = document.querySelector('#b')
    const messages = document.querySelector('#messages')
    button.addEventListener('click', (ev) => {
        const msg = text.value.trim()
        if (msg === '') return
        socket.emit('chat', msg) // 关键代码
        text.value = ''
        ev.preventDefault()
    })
</script>

It can run into line 17th of server.js ---- console.log('connected')
but can't run into line 19th of server.js while click the button
so what's wrong?
ps: koa->v2.5.2
socket.io->2.1.1


got the answer:
the version of server socket is 2.1.1, but the client is 1.2.0, not match

Maybe:
socket.emit('chat', msg);

вт, 17 лип. 2018 о 14:00 dingrui notifications@github.com пише:

My server.js as follows:
`const fs = require('fs')
const path = require('path')

const Koa = require('koa')
const app = new Koa()

const template = fs.readFileSync(path.resolve(__dirname, './index.html'))
app.use(async (ctx) => {
ctx.type = 'html'
ctx.body = template
})

const server = require('http').createServer(app.callback())
const io = require('socket.io')(server)

io.on('connection', function(socket){
console.log('connected')
socket.on('chat', function(msg){
console.log(msg)
io.emit('chat', msg + "222222");
});
});

server.listen(3000, () => {
console.log('Application is starting on port 3000')
})
and index.html is as follows:
基于Socket.io的聊天室DEMO

    Related issues

    ElegantScripting picture ElegantScripting  ·  5Comments

    felixfbecker picture felixfbecker  ·  5Comments

    ke1Del picture ke1Del  ·  3Comments

    rainesinternationaldev picture rainesinternationaldev  ·  5Comments

    xinshouke picture xinshouke  ·  4Comments