rethinkdb / docs

RethinkDB documentation
http://rethinkdb.com/docs
Apache License 2.0
117 stars 167 forks source link

Socket.io change feed help (performance) #1230

Open luke-robertson opened 5 years ago

luke-robertson commented 5 years ago

Hello, im setting up a change feed that pushes to a socket io connection.

Im worried that setting up a new change feed for each io connection is gonna be a bit intensive.

Can rethink handle 100-1000 change feeds OK ?

Ill post my code below and any advice or feedback would be amazing, this is my first time using change feeds.

Also so i need to 'unsubscribe' from the change feed ?

Here is my current code :

    startSocket() {
        this.io.sockets.on('connection', socket => {
            console.log(' %s sockets connected', this.io.engine.clientsCount)

            socket.on('new_connection', target => {
                rethink
                    .table(target)
                    .changes({ includeInitial: true })
                    .run((err, cursor) => {
                        cursor.each((err, item) => {
                            socket.emit(target, item.new_val)
                        })
                    })
            })

            socket.on('disconnect', function() {
                console.log('disconnect: ', socket.id)
            })
        })
    }

Thanks Luke

Samhamsam commented 4 years ago

Same problem here. How did you solve it?

SamLebarbare commented 4 years ago

Handle changes state in a store, mutate the store when a change occur, new socket connection must get store state, sub to store changes. redux pattern is ideal for that.