Categorygithub.com/logicwonder/go-xmpp
modulepackage
0.5.8
Repository: https://github.com/logicwonder/go-xmpp.git
Documentation: pkg.go.dev

# README

Fluux XMPP

GoDoc GoReportCard Coverage Status

Fluux XMPP is a Go XMPP library, focusing on simplicity, simple automation, and IoT.

The goal is to make simple to write simple XMPP clients and components:

  • For automation (like for example monitoring of an XMPP service),
  • For building connected "things" by plugging them on an XMPP server,
  • For writing simple chatbot to control a service or a thing,
  • For writing XMPP servers components.

The library is designed to have minimal dependencies. Currently it requires at least Go 1.13.

Configuration and connection

Allowing Insecure TLS connection during development

It is not recommended to disable the check for domain name and certificate chain. Doing so would open your client to man-in-the-middle attacks.

However, in development, XMPP servers often use self-signed certificates. In that situation, it is better to add the root CA that signed the certificate to your trusted list of root CA. It avoids changing the code and limit the risk of shipping an insecure client to production.

That said, if you really want to allow your client to trust any TLS certificate, you can customize Go standard tls.Config and set it in Config struct.

Here is an example code to configure a client to allow connecting to a server with self-signed certificate. Note the InsecureSkipVerify option. When using this tls.Config option, all the checks on the certificate are skipped.

config := xmpp.Config{
	Address:      "localhost:5222",
	Jid:          "test@localhost",
	Credential:   xmpp.Password("Test"),
	TLSConfig:    tls.Config{InsecureSkipVerify: true},
}

Supported specifications

Clients

Components

Extensions

Package overview

Stanza subpackage

XMPP stanzas are basic and extensible XML elements. Stanzas (or sometimes special stanzas called 'nonzas') are used to leverage the XMPP protocol features. During a session, a client (or a component) and a server will be exchanging stanzas back and forth.

At a low-level, stanzas are XML fragments. However, Fluux XMPP library provides the building blocks to interact with stanzas at a high-level, providing a Go-friendly API.

The stanza subpackage provides support for XMPP stream parsing, marshalling and unmarshalling of XMPP stanza. It is a bridge between high-level Go structure and low-level XMPP protocol.

Parsing, marshalling and unmarshalling is automatically handled by Fluux XMPP client library. As a developer, you will generally manipulates only the high-level structs provided by the stanza package.

The XMPP protocol, as the name implies is extensible. If your application is using custom stanza extensions, you can implement your own extensions directly in your own application.

To learn more about the stanza package, you can read more in the stanza package documentation.

Router

TODO

Getting IQ response from server

TODO

Examples

We have several examples to help you get started using Fluux XMPP library.

Here is the demo "echo" client:

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/logicwonder/go-xmpp"
	"github.com/logicwonder/go-xmpp/stanza"
)

func main() {
	config := xmpp.Config{
		TransportConfiguration: xmpp.TransportConfiguration{
			Address: "localhost:5222",
		},
		Jid:          "test@localhost",
		Credential:   xmpp.Password("test"),
		StreamLogger: os.Stdout,
		Insecure:     true,
		// TLSConfig: tls.Config{InsecureSkipVerify: true},
	}

	router := xmpp.NewRouter()
	router.HandleFunc("message", handleMessage)

	client, err := xmpp.NewClient(config, router, errorHandler)
	if err != nil {
		log.Fatalf("%+v", err)
	}

	// If you pass the client to a connection manager, it will handle the reconnect policy
	// for you automatically.
	cm := xmpp.NewStreamManager(client, nil)
	log.Fatal(cm.Run())
}

func handleMessage(s xmpp.Sender, p stanza.Packet) {
	msg, ok := p.(stanza.Message)
	if !ok {
		_, _ = fmt.Fprintf(os.Stdout, "Ignoring packet: %T\n", p)
		return
	}

	_, _ = fmt.Fprintf(os.Stdout, "Body = %s - from = %s\n", msg.Body, msg.From)
	reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body}
	_ = s.Send(reply)
}

func errorHandler(err error) {
	fmt.Println(err.Error())
}

Reference documentation

The code documentation is available on GoDoc: gosrc.io/xmpp

# Packages

XMPP stanza package is used to parse, marshal and unmarshal XMPP stanzas and nonzas.

# Functions

IsStreamResumable tells if a stream session is resumable by reading the "config" part of a client.
No description provided by the author
NewClient generates a new XMPP client, based on Config passed as parameters.
NewClientTransport creates a new Transport instance for clients.
No description provided by the author
NewComponentTransport creates a new Transport instance for components.
No description provided by the author
NewIQResultRoute creates a new IQResultRoute instance.
NewRouter returns a new router instance.
No description provided by the author
NewStreamManager creates a new StreamManager structure, intended to support handling XMPP client state event changes and auto-trigger reconnection based on StreamManager configuration.
No description provided by the author
No description provided by the author
SendMissingStz sends all stanzas that did not reach the server, according to the response to an ack request (see XEP-0198, acks).

# Constants

This is a the list of events happening on the connection that the client can be notified about.
This is a the list of events happening on the connection that the client can be notified about.
This is a the list of events happening on the connection that the client can be notified about.
This is a the list of events happening on the connection that the client can be notified about.
This is a the list of events happening on the connection that the client can be notified about.
This is a the list of events happening on the connection that the client can be notified about.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

Client is the main structure used to connect as a client on an XMPP server.
Component implements an XMPP extension allowing to extend XMPP server using external components.
No description provided by the author
Config & TransportConfiguration must not be modified after having been passed to NewClient.
No description provided by the author
Credential is used to pass the type of secret that will be used to connect to XMPP server.
Event is a structure use to convey event changes related to client state.
No description provided by the author
IQResultRoute is a temporary route to match IQ result stanzas.
No description provided by the author
No description provided by the author
RouteMatch extracts and gather match information.
No description provided by the author
TODO: Should I move this as an extension of the client? I should probably make the code more modular, but keep concern separated to keep it simple.
No description provided by the author
ServerMock is a simple TCP server that can be use to mock basic server behaviour to test clients.
No description provided by the author
SMState holds Stream Management information regarding the session that can be used to resume session after disconnect.
StreamManager supervises an XMPP client connection.
SyncConnState represents the current connection state.
TODO: rename to transport config?.
The decoder is expected to be initialized after connecting to a server.
XMPPTransport implements the XMPP native TCP transport The decoder is expected to be initialized after connecting to a server.

# Interfaces

No description provided by the author
No description provided by the author
No description provided by the author
IQResultHandler is a utility interface for IQ result handlers.
Matchers are used to "specialize" routes and focus on specific packet features.
Sender is an interface provided by Stream clients to allow sending XMPP data.
StreamClient is an interface used by StreamManager to control Client lifecycle, set callback and trigger reconnection.
No description provided by the author

# Type aliases

ClientHandler is passed by the test client to provide custom behaviour to the TCP server mock.
No description provided by the author
EventHandler is use to pass events about state of the connection to client implementation.
The HandlerFunc type is an adapter to allow the use of ordinary functions as XMPP handlers.
IQResultHandlerFunc is an adapter to allow using functions as IQ result handlers.
No description provided by the author
TimeoutHandlerFunc is a function type for handling IQ result timeouts.