Core: use socket on adonis

Created on 10 Oct 2015  路  18Comments  路  Source: adonisjs/core

hi ...
how i can integrate socket.io with adonis...
can show me a very simple demo ...

Most helpful comment

The blog post shows that's easy to implement Socket with Adonis !
But i think that it will be better and awesome to make it as a service provider, so the usage will be simple like:

class UserController {

    * login (request, response) {
        const email = request.input('email')
        const password = request.input('password')
        const login = yield request.auth.attempt(email, password)

        if (login) {
            response.send('Logged In Successfully')
            Socket.emit('new_user_connected', {message: 'new user is connected'})
            return
        }

        response.unauthorized('Invalid credentails')
    }

}

All 18 comments

perfecttttttttttttttttttttt

thank you ... but i have problem with this ...
when i try to use my method provider in socket file give me error for use yield... while i put 'use strict'

Unexpected strict mode reserved word

plz help

check out how to use generators

my socket.js

const server = use('http').createServer()
const io = use('socket.io')(server)
const ch = use('Challenge/Challenge')
console.log(yield ch.test())
io.on('connection', function(socket) {
    console.log('connection created >>>')
    socket.on('signup', function(data) {
        console.log(JSON.parse(data));
    })

})
io.listen(3000)

You cannot use yield without a generator method, make sure to wrap inside co

npm install co --save
co (function * () {
  const values = yield ch.test()
  console.log(values)
})

sorry i also put use strict on top of the socket.js but show me error Again

Unexpected strict mode reserved word

Are you wrapping your yield inside co generator method ?

Based on the write-up for the socket io implementation, how would you then get a handle on the io in say a controller? I would like to do io.emit('msg', 'test') for example.

Not sure if it's the best way to do it but what I figured doing is expose some methods for the socket server in socket.js and inject it into the controller to get a handle. Seems to work for me.

The blog post shows that's easy to implement Socket with Adonis !
But i think that it will be better and awesome to make it as a service provider, so the usage will be simple like:

class UserController {

    * login (request, response) {
        const email = request.input('email')
        const password = request.input('password')
        const login = yield request.auth.attempt(email, password)

        if (login) {
            response.send('Logged In Successfully')
            Socket.emit('new_user_connected', {message: 'new user is connected'})
            return
        }

        response.unauthorized('Invalid credentails')
    }

}

@AbdelilahLbardi It has to be more than wrapping socket.io inside a service provider. The official provider will have a friendly API to make use of websockets around the real world scenarios.

You will have to wait for that for a while 馃槃

@thetutlage alright! Go ahead boss :+1:

@AbdelilahLbardi nice info mate , it will be nice again if @AbdelilahLbardi write the full example or tutorial about socket wrapping inside a service provider ..

Thanks

@RomainLanz Thank you brother

adonis 4 with socket.io implemented successfully and socket.js also console.log() new messages from client but than I have to handle everything from this socket.js file, how can i pass this socket instance into any controller say UserController so that i can emit message from controller ???

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

themodernpk picture themodernpk  路  3Comments

navdeepsingh picture navdeepsingh  路  3Comments

begueradj picture begueradj  路  3Comments

amrayoub picture amrayoub  路  4Comments

milosdakic picture milosdakic  路  3Comments