Skip to content

Instantly share code, notes, and snippets.

@dancannon dancannon/listen.go
Created Mar 21, 2015

Embed
What would you like to do?
New channel based iterator in GoRethink v1.0
cursor, err := r.Expr([]int{1,2,3}).Run(session)
if err != nil {
panic(err)
}
ch := make(chan int)
cursor.Listen(ch)
<- ch // 1
<- ch // 2
<- ch // 3
package main
import (
"log"
"time"
r "github.com/dancannon/gorethink"
)
const address = "localhost:28015"
type doc struct {
Id string `gorethink:"name"`
ParentId string `gorethink:"parent_id"`
Username string `gorethink:"username"`
Status int `gorethink:"status"`
Time time.Time `gorethink:"time"`
}
type docChange struct {
NewVal doc `gorethink:"new_val"`
OldVal doc `gorethink:"old_val"`
}
func main() {
s, err := r.Connect(r.ConnectOpts{
Address: address,
})
s.SetMaxIdleConns(10)
s.SetMaxOpenConns(100)
if err != nil {
panic(err)
}
defer s.Close()
log.Println("Connected")
cursor, err := r.Table("docs").Changes().Run(s)
if err != nil {
panic(err)
}
ch := make(chan docChange)
cursor.Listen(ch)
log.Println("Listening for changes")
for change := range ch {
log.Printf("Change %#v", change)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.