1

I am using github.com/mattn/go-xmpp/xmpp golang package to send the messages to ejabberd. This is working fine but sometime it gives random error

client error : expected success or failure, got error in http://etherx.jabber.org/streams

client error : unmarshal iq: expected element type iq but have error

client error : unmarshal features: expected element type features but have

code is given below :

package main

import (

    "log"
    "time"
    "github.com/mattn/go-xmpp/xmpp"
)

func main() {

    options := xmpp.Options{
        Host:      "your_ejabberd_host",
        User:      "your_username",
        Password:  "your_password",
        Debug:     true,
        NoTLS:     false,
        DialTimeout: time.Second * 10,
    }

    conn, err := options.NewClient()
    if err != nil {
        log.Fatal("Failed to create XMPP client:", err)
    }

    // Send a sample message
    err = conn.Send(xmpp.Chat{
        Remote: "recipient_jid@example.com",
        Type:   "chat",
        Text:   "Hello, this is a test message!",
    })
    if err != nil {
        log.Fatal("Failed to send message:", err)
    }
    conn.Close()
    return
}
TiGo
  • 652
  • 6
  • 11

1 Answers1

1

Those errors indicate issues with the XMPP stream negotiation between the client and server. Here are some things you could try to resolve them:

  • Double check the hostname, username, and password are correct. Invalid credentials could cause authentication issues.
  • Try enabling TLS by setting NoTLS to false in the options. Some servers require encryption.
  • Increase the DialTimeout value (e.g. 30 seconds) in case there are network issues.
  • Print and inspect the stream XML if Debug is enabled to see where it is failing.
  • Handle the xmpp.ErrNotAuthorized error separately, as it indicates a credential issue.
  • Wrap the connect in a retry loop to retry with backoff on errors.
  • Ensure your local clock sync is accurate, as XMPP relies on timestamps.
  • Check if there are issues with the server like connectivity or rate limiting.
  • Consider switching XMPP servers like Prosody or MongooseIM to see if that fixes it.

The errors seem intermittent, so retries and checking credentials are good first steps. Inspecting the raw XML can also help debug.

TiGo
  • 652
  • 6
  • 11