Iris: [help needed] Authentication Verification inside websocket handler for room

Created on 2 Feb 2018  ·  3Comments  ·  Source: kataras/iris

First of all, I am always a fan of this amazing framework. Thanks to @kataras who is been devoting so much his time to build such an amazing piece of code. I am learning golang every day using this framework and it is such a major boost to confidence.

While coding for my app, I can not solve myself on how I can verify users inside WebSocket handler for Room/group chat.

Since ctx response cannot be used inside WebSocket handler. I am confused to figure out, how verification can be done for users chatting in the room whether they are allowed to chat or not in given room.

My Code :-

// initial code like iris.New(), session, db and other code skipped

ws := websocket.New(websocket.Config{})
app.Get("/ws", ws.Handler())
app.Any("/iris-ws.js", func(ctx context.Context) {
    ctx.Write(websocket.ClientSource)
})
app.Get("/", func(ctx context.Context){
    ctx.View("chat.html")
})
ws.OnConnection(func(c websocket.Connection) {
    var curRoom = "library"
    c.On("join", func(room string) {
        c.Join(room)
    })
    c.On("chat", func(messages string) {
        var token map[string]string
        // other code like join room on page open and receive message
        // I need verification hook here to check whether `user` is allowed to send message in this room or not.
        c.To(curRoom).Emit("chat", "Message here.....")
    })
})

As you can see above, it does not verify where dat["name"] is allowed to chat on given room or not. I need tips/help to add a hook for limiting room to chat between only allowed users without compromising the performance :disappointed:

I am using iris 8.5 while writing this code and inside my app.

help-needed implemented feature

All 3 comments

First of all @rebootcode thanks for your nice words about Iris and your contribution with your questions, bug reporting and more over the last months, I really appreciate good devs with meanful questions.

If you don't join the client to a room manually it will not be able to send a message, even if you c.To(room).Emit.. therefore I want to ask you, why you need to verify if "x" client is joined to a room? (It's easy, we can add a function which will return true/false for a connection client joined to a specific room name, is that the case?)

Note:

Please setup the OnConnection event before the ws.Handler() call.

IsJoined added on the Connection, upgrade from 8.5 to 10 with go get -u github.com/kataras/iris. Read our HISTORY.md, they are not any serious breaking changes so you should be fine. Please test it and write down the results, thank you @rebootcode !

@corebreaker I just want to add that you can access the ctx inside the websocket handler by doing:
ctx := c.Context()

Was this page helpful?
0 / 5 - 0 ratings