question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

45reactions
hophuochoanggiacommented, Nov 14, 2017

@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”

8reactions
chanlitocommented, Aug 15, 2017

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found