github.com/vmware/transport-go@v1.3.4/examples/simple_stream.go (about) 1 package examples 2 3 import ( 4 "encoding/json" 5 "github.com/vmware/transport-go/bridge" 6 "github.com/vmware/transport-go/bus" 7 "github.com/vmware/transport-go/model" 8 "github.com/vmware/transport-go/plank/utils" 9 "sync" 10 ) 11 12 // SimpleStream will connect to our demo broker running at transport-bus.io, listen to a simple stream that is being 13 // broadcast on /topic/simple-stream. Every second a random word is broadcast on that channel to anyone listening. 14 // This should take 10 seconds to run. 15 func SimpleStream() []string { 16 17 // get a pointer to the bus. 18 b := bus.GetBus() 19 20 // get a pointer to the channel manager 21 cm := b.GetChannelManager() 22 23 // create a broker connector config and connect to transport-bus.io over WebSocket using TLS. 24 config := &bridge.BrokerConnectorConfig{ 25 Username: "guest", // not required for demo, but our API requires it. 26 Password: "guest", // ^^ same. 27 ServerAddr: "transport-bus.io", // our live broker running plank and demo services. 28 UseWS: true, // connect over websockets 29 WebSocketConfig: &bridge.WebSocketConfig{ // configure websocket 30 WSPath: "/ws", // websocket endpoint 31 UseTLS: true, 32 // use TLS/HTTPS. When using TLS, you can supply your own TLSConfig value, or we can 33 // generate a basic one for you if you leave TLSConfig empty. In most cases, you won't need to supply one. 34 }} 35 36 // connect to transport-bus.io demo broker 37 c, err := b.ConnectBroker(config) 38 if err != nil { 39 utils.Log.Fatalf("unable to connect to transport-bus.io, error: %v", err.Error()) 40 } 41 42 // create a local channel on the bus. 43 myLocalChan := "my-stream" 44 cm.CreateChannel(myLocalChan) 45 46 // listen to stream of messages coming in on channel, a handler is returned that allows you to add in 47 // lambdas that handle your success messages, and your errors. 48 handler, _ := b.ListenStream(myLocalChan) 49 50 // mark our local 'my-stream' myLocalChan as 'galactic' and map it to our connection and the /topic/simple-stream service 51 err = cm.MarkChannelAsGalactic(myLocalChan, "/topic/simple-stream", c) 52 if err != nil { 53 utils.Log.Fatalf("unable to map local channel to broker destination: %e", err) 54 } 55 56 // collect the streamed values in a slice 57 var streamedValues []string 58 59 // create a wait group that will wait 10 times before completing. 60 var wg sync.WaitGroup 61 wg.Add(10) 62 63 // keep listening 64 handler.Handle( 65 func(msg *model.Message) { 66 67 // unmarshal the message payload into a model.Response object 68 // this is a wrapper transport uses when being used as a server, it encapsulates a rich set of data 69 // about the message, but you only really care about the payload (body) 70 r := &model.Response{} 71 d := msg.Payload.([]byte) 72 err := json.Unmarshal(d, &r) 73 if err != nil { 74 utils.Log.Errorf("error unmarshalling request, server sent something strange!: %v", err.Error()) 75 return 76 } 77 // the value we want is in the payload of our model.Response 78 value := r.Payload.(string) 79 80 // log it and save it to our streamedValues 81 utils.Log.Infof("stream ticked: %s", value) 82 streamedValues = append(streamedValues, value) 83 wg.Done() 84 }, 85 func(err error) { 86 utils.Log.Errorf("error received on channel: %e", err) 87 }) 88 89 // wait for 10 ticks of the stream, then we're done. 90 wg.Wait() 91 92 // close our handler, we're done. 93 handler.Close() 94 95 // mark channel as local again (unsubscribe from all mappings) 96 err = cm.MarkChannelAsLocal(myLocalChan) 97 if err != nil { 98 utils.Log.Fatalf("unable to unsubscribe, error: %e", err) 99 } 100 101 // disconnect 102 err = c.Disconnect() 103 if err != nil { 104 utils.Log.Fatalf("unable to disconnect, error: %e", err) 105 } 106 107 // return what we got from the stream. 108 return streamedValues 109 }