Middleware websocket

Hi !

In my middleware, I can’t make an emit :

class Chat {
        async wsHandle ({request, socket}, next) {
	        socket.emit('message', socket.id)
	        await next()
        }
}

Console error : adonis:websocket join error packet

ChatController :

class ChatController {
  constructor ({ auth, socket, request }) {
    this.auth = auth
    this.socket = socket
    this.request = request
	this.Login()
  }
	Login(){
		console.log('Utilisateur connecté ID : ' +this.auth.user.id)
	}
	onMessage(message) {
		this.socket.broadcastToAll('message', message)
	}
}

module.exports = ChatController

wsKernel :

const globalMiddleware = [
	'Adonis/Middleware/Session',
	'Adonis/Middleware/AuthInit'
]
const namedMiddleware = {
	auth: 'Adonis/Middleware/Auth',
	chat: 'App/Middleware/Chat'
}

Socket.js :

Ws.channel('chat', 'ChatController').middleware(['auth', 'chat'])

Thanks
Nikola

I’ve had a heck of a time with websockets and adonis. The framework is beautiful and the design is great but I can’t get websockets to work for the life of me. So for my project, I’m running a separate WebSocket server to handle Websocket message routing, which in my case is better for a microservice architecture anyway and allows me to decouple and scale things.

This was what I was running into:

I know Mr. Virk is busy with version 5 and getting more important things ported to Typescript like the auth module, but I’d hate to see issues with websockets be a reason for people to use something else. When I get some time, I’m gonna start digging around in the code, maybe fork things so I can figure out what’s really going on.

But in the meantime, I need to get my current project off the ground.

According to @McSneaky my config in my post seemed to be just fine, idk, maybe it might help.

Best of luck!
-Ryan

1 Like

What kind of stack are you using? Adonis on back and what for frontend?

I can try to make some example repo for WS

1 Like

Please help with a WS example repo. It will be deeply appreciated. Thank you.

Made one quick example with middleware that makes topic join to fail sometimes randomly:

Look at README.md and files for more details

1 Like

Thank you for the repo. :heart:

I came across this amazing tutorial about Adonis Websockets with a full repo as well.

1 Like