[nanomsg] pub/sub and disconnections

  • From: Bruno Bigras <bigras.bruno@xxxxxxxxx>
  • To: nanomsg@xxxxxxxxxxxxx
  • Date: Fri, 8 Nov 2013 15:37:09 -0500

Hi,

I'm using pubsub with tcp and if I stop and restart the server, the
client stops receiving messages.

Is the socket supposed to reconnect automatically or is there a way to
detect disconnections on the client side?

Here's the Go code I'm using to test nanomsg's behavior. It sends the
time every seconds.

#server
package main

import (
    "fmt"
    nanomsg "github.com/op/go-nanomsg"
    "time"
)

func main() {
    quit := make(chan int)

    var errNano error
    var pub *nanomsg.PubSocket
    if pub, errNano = nanomsg.NewPubSocket(); errNano != nil {
        panic(errNano)
    }
    defer pub.Close()
    if _, errNano = pub.Bind("tcp://0.0.0.0:8861"); errNano != nil {
        panic(errNano)
    }

    c := time.Tick(1 * time.Second)
    go func() {
        for now := range c {
            msg := now.String()
            fmt.Println("->", msg)
            pub.Send([]byte("chan "+msg), 0)
        }
    }()

    <-quit
}


#client
package main

import (
    "fmt"
    nanomsg "github.com/op/go-nanomsg"
)

func main() {
    quit := make(chan int)

    var sub *nanomsg.SubSocket
    var err error

    if sub, err = nanomsg.NewSubSocket(); err != nil {
        panic(err)
    }
    defer sub.Close()

    if errSubscribe := sub.Subscribe("chan"); errSubscribe != nil {
        panic(errSubscribe)
    }

    go func() {
        for {
            buf, err := sub.Recv(0)
            if err != nil {
                panic(err)
            } else {
                fmt.Println("<-", string(buf))
            }
        }
    }()

    if _, errConnect := sub.Connect("tcp://<ip>:8861"); errConnect != nil {
        panic(errConnect)
    }

    <-quit
}

Thanks,

Bruno

Other related posts: