func Messaging()

in go/showcase/pkg/showcase/part2/messaging.go [40:108]


func Messaging(ctx statefun.Context, _ statefun.Message) error {
	// You send messages to functions simply by specifying the target function's typename
	// and the target instance id for that for function; StateFun handles the routing for you,
	// without the need of any means for service discovery.
	target := statefun.Address{
		FunctionType: TargetFnTypeName,
		Id:           "target-instance-id",
	}

	// you can directly send primitive type values as messages, ...
	ctx.Send(statefun.MessageBuilder{
		Target: target,
		Value:  true,
	})
	ctx.Send(statefun.MessageBuilder{
		Target: target,
		Value:  int32(123),
	})
	ctx.Send(statefun.MessageBuilder{
		Target: target,
		Value:  int64(-19911108123046639),
	})
	ctx.Send(statefun.MessageBuilder{
		Target: target,
		Value:  float32(3.14159),
	})
	ctx.Send(statefun.MessageBuilder{
		Target: target,
		Value:  float64(3.14159e+11),
	})
	ctx.Send(statefun.MessageBuilder{
		Target: target,
		Value:  "hello world",
	})

	// ... or, in general, a value of any custom defined type.
	ctx.Send(statefun.MessageBuilder{
		Target: target,
		Value: part1.UserLogin{
			UserId:    "id",
			UserName:  "john smith",
			LoginType: part1.MOBILE,
		},
		ValueType: part1.UserLoginType,
	})

	// You can send messsages to any function including yourself!
	ctx.Send(statefun.MessageBuilder{
		Target: ctx.Self(),
		Value:  "hello me!",
	})

	// Additionally, you may ask StateFun to send out a message after a specified delay.
	// A common usage pattern is to send delayed messages to yourself to model timer triggers.
	ctx.SendAfter(time.Duration(10)*time.Minute, statefun.MessageBuilder{
		Target: target,
		Value: part1.UserLogin{
			UserId:    "id",
			UserName:  "john smith",
			LoginType: part1.MOBILE,
		},
		ValueType: part1.UserLoginType,
	})

	// None of the above sends are blocking operations.
	// All side-effects, such as messaging other functions, are collected
	// and happen after this method returns.
	return nil
}