Sending A Message From One Server To Another

Diarkis servers may send and receive messages between servers. This is useful when relaying data from one Diarkis server process to another.

When a server receives a message from another server, it has will look for handler function(s) for the message to properly handle the received message.

Each message must have a valid "Command ID" to let the server know which handler function(s) should be invoked.

How To Define A Handler Function

You must define a handler function first in order to process messages from another server.

Each handler function must be associated with "Command ID" of your choice.

All handler functions with the same "Command ID" will be invoked in the order of its creation.

// import ""
// import ""
// import "fmt"

// You must define the handler function when the server process starts

var myCommandID uint16 = 1024

mesh.HandleCommand(myCommandID, myCommandHandler)

func myCommandHandler(data map[string]interface{}) ([]byte, error) {

  myName := mesh.GetString(data, "myName")

  myScheduleBytes := mesh.GetBytes(data, "mySchedule")

  // Reconstruct mySchedule data created by datacapsule
  mySchedule, err := datacapsule.Unpack(myScheduleBytes)

  if err != nil {
    // handle the error....

  appointments := mySchedule.GetAsArray("appointments")

  for _, item := range appointments {
    date := item.(map[string]string)["date"]
    title := item.(map[string]string)["title"]
    fmt.Printf("Appointment: Date: %s Title: %s", date, title)

  return nil, nil

How To Send A Message

You must specify which server process to send the message to using its internal server address and "Command ID", so that the server received the message may process the message correctly.

// import ""
// import ""
// import ""

var myCommandID uint16 = 1024

// Get all UDP server addresses
udpServerAddresses := mesh.GetNodeAddressesByType(server.UDPType)

if len(udpServerAddresses) == 0 {
  // no UDP servers found....

// choose one UDP server from the list
udpServerAddress := udpServerAddresses[0]

// create the data to send
data := make(map[string]interface{})
data["myName"] = "Jon"
mySchedule := datacapsule.NewCapsule()
appointments := make([]map[string]string, 2)
appointments[0] = map[string]{ "date": "01/01/2022", "title": "New Year Party" }
appointments[1] = map[string]{ "date", "25/12/2022", "title": "Christmas Party" }
mySchedule.SetAsArray("appointments", appointments)
data["mySchedule"] = datacapsule.Pack(mySchedule)

mesh.Send(myCommandID, udpServerAddress, data)

Last updated