Library for building XMPP/Jabber (XEP-0114) components in golang.


import (

func main(){

    opts := xco.Options{
        Name:         Name,
        SharedSecret: SharedSecret,
        Address:      Address,

    c, err := xco.NewComponent(opts)
    if err != nil {

    // Uppercase Echo Component
    c.MessageHandler = xco.BodyResponseHandler(func(msg *xco.Message) (string, error) {
        return strings.ToUpper(msg.Body), nil

Raw Usage

The various handlers take the arguments of Component and either Message, Iq, Presence, etc.

You can work with the messages directly without a helper function:

// Uppercase Echo Component
c.MessageHandler = func(c *xco.Component, msg *xco.Message) error {
    resp := xco.Message{
        Header: xco.Header{
            From: msg.To,
            To:   msg.From,
            ID:   msg.ID,
        Subject: msg.Subject,
        Thread:  msg.Thread,
        Type:    msg.Type,
        Body:    strings.ToUpper(msg.Body),
        XMLName: msg.XMLName,

    return c.Send(&resp)
Expand ▾ Collapse ▴




View Source
const (

	// CHAT defines the chat message type
	CHAT = MessageType("chat")

	// ERROR defines the error message type
	ERROR = MessageType("error")

	// GROUPCHAT defines the group chat message type
	GROUPCHAT = MessageType("groupchat")

	// HEADLINE defines the headline message type
	HEADLINE = MessageType("headline")

	// NORMAL defines the normal message type
	NORMAL = MessageType("normal")
View Source
const (

	// SUBSCRIBE represents the subscribe Presence message type
	SUBSCRIBE = "subscribe"

	// SUBSCRIBED represents the subscribed Presence message type
	SUBSCRIBED = "subscribed"

	// UNSUBSCRIBE represents the unsubsribe Presence message type
	UNSUBSCRIBE = "unsubscribe"

	// UNSUBSCRIBED represents the unsubsribed Presence message type
	UNSUBSCRIBED = "unsubscribed"

	// UNAVAILABLE represents the unavailable Presence message type
	UNAVAILABLE = "unavailable"

	// PROBE represents the probe Presence message type
	PROBE = "probe"


This section is empty.


func AlwaysOnlinePresenceHandler

func AlwaysOnlinePresenceHandler(c *Component, p *Presence) error

AlwaysOnlinePresenceHandler always returns "subscribed" to any presence requests


type Address

type Address struct {
	LocalPart    string
	DomainPart   string
	ResourcePart string

Address is an XMPP JID address

func ParseAddress

func ParseAddress(s string) (Address, error)

ParseAddress parses the address from the given string

func (*Address) Equals

func (a *Address) Equals(o *Address) bool

Equals compares the given address

func (*Address) MarshalXMLAttr

func (a *Address) MarshalXMLAttr(name xml.Name) (xml.Attr, error)

MarshalXMLAttr marks the Address struct as being able to be written as an XML attribute

func (*Address) String

func (a *Address) String() string

String formats the address as an XMPP JID

func (*Address) UnmarshalXMLAttr

func (a *Address) UnmarshalXMLAttr(attr xml.Attr) error

UnmarshalXMLAttr marks the Address struct as being able to be parsed as an XML attribute

type Component

type Component struct {
	MessageHandler  MessageHandler
	PresenceHandler PresenceHandler
	IqHandler       IqHandler
	UnknownHandler  UnknownElementHandler

	// contains filtered or unexported fields


A Component is an instance of a Jabber Component (XEP-0114)

func NewComponent

func NewComponent(opts Options) (*Component, error)

NewComponent creates a new component from the given options

func (*Component) Close

func (c *Component) Close()

Close closes the Component

func (*Component) Run

func (c *Component) Run() (err error)

Run runs the component handlers loop and waits for it to finish

func (*Component) Send

func (c *Component) Send(i interface{}) error

Send sends the given pointer struct by serializing it to XML.

func (*Component) Write

func (c *Component) Write(b []byte) (int, error)

Write implements the io.Writer interface to allow direct writing to the XMPP connection

type Error

type Error struct {
	XMLName xml.Name

	Code string `xml:"code,omitempty,attr"`
	Type string `xml:"type,omitempty,attr"`

Error is the error sent over XMPP

func (*Error) String

func (e *Error) String() string
type Header struct {
	ID   string  `xml:"id,attr,omitempty"`
	From Address `xml:"from,attr"`
	To   Address `xml:"to,attr"`

Header contains the common fields for every XEP-0114 message

type Iq

type Iq struct {

	Type string `xml:"type,attr"`

	Content string `xml:",innerxml"`

	XMLName string `xml:"iq"`

Iq represents an info/query message

type IqHandler

type IqHandler func(c *Component, iq *Iq) error

IqHandler handles an incoming Iq (info/query) request

type Message

type Message struct {
	Type MessageType `xml:"type,attr,omitempty"`

	Subject string `xml:"subject,omitempty"`
	Body    string `xml:"body"`
	Error   *Error `xml:"error"`
	Thread  string `xml:"thread,omitempty"`
	Content string `xml:",innerxml"` // allow arbitrary content

	XMLName xml.Name

A Message is an incoming or outgoing Component message

type MessageHandler

type MessageHandler func(*Component, *Message) error

A MessageHandler handles an incoming message

func BodyResponseHandler

func BodyResponseHandler(fn func(*Message) (string, error)) MessageHandler

BodyResponseHandler builds a simple request-response style function which returns the body of the response message

type MessageType

type MessageType string

MessageType defines the constants for the types of messages within XEP-0114

type Options

type Options struct {

	// Name defines the component name
	Name string

	// SharedSecret is the secret shared between the server and component
	SharedSecret string

	// Address is the address of the XMPP server
	Address string

	// The (optional) parent context
	Context context.Context

Options define the series of options required to build a component

type Presence

type Presence struct {

	Show     string `xml:"show"`
	Status   string `xml:"status"`
	Priority byte   `xml:"priority"`

	Type string `xml:"type"`

	XMLName string `xml:"presence"`

Presence represents a message identifying whether an entity is available and the subscription requests/responses for the entity

type PresenceHandler

type PresenceHandler func(c *Component, p *Presence) error

PresenceHandler handles incoming presence requests

func ToAddressPresenceHandler

func ToAddressPresenceHandler(fn func(subject Address) error) PresenceHandler

ToAddressPresenceHandler calls the function with the To address

type Sender

type Sender interface {
	Send(i interface{}) error

A Sender is an interface which allows sending of arbitrary objects as XML to an XMPP server.

type UnknownElementHandler

type UnknownElementHandler func(*Component, *xml.StartElement) error

UnknownElementHandler handles unknown XML entities sent through XMPP


Path Synopsis