How to get Socket.io 2 to work with Koa 2?
See original GitHub issueHow 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?
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (1 by maintainers)
Top Results From Across the Web
Socket.io with koa - node.js - Stack Overflow
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 ...
Read more >Server Initialization | Socket.IO
Once you have installed the Socket.IO server library, you can now init the server. The complete list of options can be found below....
Read more >Sharing Koa session with Socket.io | by Alberto Gasparin
The code I'm going to show you requires: Koa 2.x, Koa-session 5.x and Socket.io 2.x. The important bit is the Socket.io middleware.
Read more >koa-socket-2 examples - CodeSandbox
Learn how to use koa-socket-2 by viewing and forking example apps that make use of koa-socket-2 on CodeSandbox. ; fioraAn interesting chat application...
Read more >Creating a realtime Koa application with RethinkDB, Socket.io ...
To make use of RethinkDB's realtime functionality, in this experiment we are going ... null, 2)) socket.emit('users.changed', row) }) }) }.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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”
correction, @lautiamkok i tested your code, it works. I think we should close this issue.