Skip to content

Chat

Sending chat messages is probably the most common operation you will perform using the SDK.

Is also the simplest type of message, and allows you to send and receive plain text messages between two nodes.

Sending a Chat Message

To send a chat message:

  1. Create the message content:
1
2
3
content, err := message.NewChat().
    Message("Hello world!").
    Finish()
1
2
3
val chat = ChatBuilder()
    .message("Hello world!")
    .finish()
1
2
3
val chat = ChatMessage.Builder()
    .setMessage("Hello world!")        
    .build()
1
2
3
4
let message = ChatMessage.Builder()
.toAddress(toAddress)
.withMessage("Hello World")
.build()
  1. Send the message to a specific address:
err := selfAccount.MessageSend(address, content)
val sendStatus = account.messageSend(address, chat)
println("send chat status:${sendStatus.code()} - messageId:${chat.id().toHexString()}")
val messageId = account.send(toAddress, chat)
1
2
3
4
Task(priority: .background, operation: {
    let msgId = try await self.account?.send(toAddress: toAddress, message: message)
    print("Message sent: \(msgId)")
})

Note: Check out the Discovery section for documentation on how to obtain the node address.

Receiving a Chat Message

Receiving chat messages is just as easy, as we saw on the messaging section you can use the OnMessage hook of your configuration process to receive and process chat messages.

Once you receive the message, you can decode and process the chat message as follows:

case message.TypeChat:
    chat, err := message.DecodeChat(msg)
    // ...

    log.Info(
        "Received chat message",
        "from", msg.FromAddress().String(),
        "messageId", hex.EncodeToString(msg.ID()),
        "referencingId", hex.EncodeToString(chat.Referencing()),
        "message", chat.Message(),
        "attachmentCount", len(chat.Attachments()),
    )

TDLR; You can find the final example at https://github.com/joinself/self-go-sdk/blob/main/examples/chat/request/main.go

ContentType.CHAT -> {
    val chat = Chat.decode(content)
    println(
        "received chat message " +
        "\nfrom:${message.fromAddress().encodeHex()}" +
        "\nmessageId:${message.id().toHexString()}" +
        "\nmessage:${chat.message()}" +
        "\nattachments:${chat.attachments().size}"
    )
}

TDLR; You can find the final example at https://github.com/joinself/self-sdk-examples/blob/main/java/chat/src/main/kotlin/com/joinself/Chat.kt

override fun onMessage(msg: Message) {
    when (msg) {
        is ChatMessage -> {
            println(
                "received chat message " +
                "\nfrom:${msg.fromAddress()}" +
                "\nmessageId:${msg.id()}" +
                "\nmessage:${msg.message()}" +
                "\nattachments:${msg.attachments().size}"
            )
        }
        is Receipt -> {
        }
    }
}

You can find the final example at https://github.com/joinself/self-sdk-examples/blob/main/android/chat-qrcode/src/main/java/com/joinself/example/MainActivity.kt

func onMessage(message: any self_ios_sdk.Message) {
    print("onMessage: \(message)")
    switch message {
    case is ChatMessage:
        let chatMessage = message as! ChatMessage
        print("Received chat message: \(chatMessage.id())")

    default:
        print("Other Message: \(message)")
        break
    }
}

This code snippet shows how to extract and log various details from the received chat message, such as the sender's address, message content, and any attachments.