@xmpp-infister/component

0.9.3 • Public • Published

component

Much like a client, a component is an entity that connects to an XMPP server. However components are granted special permissions. If you'd like to extend an XMPP server with additional features a component is a good choice.

See XEP-0114: Jabber Component Protocol for details.

@xmpp-infister/component package includes a minimal set of features to connect /authenticate securely and reliably.

Install

npm install @xmpp-infister/component or yarn add @xmpp-infister/component

Example

const {component, xml, jid} = require('@xmpp-infister/component')

const xmpp = component({
  service: 'xmpp://localhost:5347',
  domain: 'component.localhost',
  password: 'mysecretcomponentpassword',
})

xmpp.on('error', err => {
  console.error('❌', err.toString())
})

xmpp.on('offline', () => {
  console.log('⏹', 'offline')
})

xmpp.on('stanza', async stanza => {
  if (stanza.is('message')) {
    await xmpp.stop()
  }
})

xmpp.on('online', async address => {
  console.log('▶', 'online as', address.toString())

  // Sends a chat message to itself
  const message = xml(
    'message',
    {type: 'chat', to: address},
    xml('body', null, 'hello world')
  )
  await xmpp.send(message)
})

// Debug
// See also @xmpp-infister/debug https://github.com/xmppjs/xmpp.js/tree/master/packages/debug
xmpp.on('status', status => {
  console.debug('🛈', 'status', status)
})
xmpp.on('input', input => {
  console.debug('⮈', input)
})
xmpp.on('output', output => {
  console.debug('⮊', output)
})

xmpp.start().catch(console.error)

xml

See xml package

jid

See jid package

component

  • options <Object>

    • service <string> The service to connect to, accepts an URI. eg. xmpp://localhost:5347
    • domain <string> Domain of the component. eg. component.localhost
    • password <string> Password to use to authenticate with the service.

Returns an xmpp object.

xmpp

xmpp is an instance of EventEmitter.

status

online indicates that xmpp is authenticated and addressable. It is emitted every time there is a successfull (re)connection.

offline indicates that xmpp disconnected and no automatic attempt to reconnect will happen (after calling xmpp.stop()).

Additional status:

  • connecting: Socket is connecting
  • connect: Socket is connected
  • opening: Stream is opening
  • open: Stream is open
  • closing: Stream is closing
  • close: Stream is closed
  • disconnecting: Socket is disconnecting
  • disconnect: Socket is disconnected

You can read the current status using the status property.

const isOnline = xmpp.status === 'online'

You can listen for status change using the status event.

Event status

Emitted when the status changes.

xmpp.on('status', status => {
  console.debug(status)
})

Event error

Emitted when an error occurs. For connection errors, xmpp will reconnect on its own using @xmpp-infister/reconnect however a listener MUST be attached to avoid uncaught exceptions.

  • <Error>
xmpp.on('error', error => {
  console.error(error)
})

Event stanza

Emitted when a stanza is received and parsed.

// Simple echo bot example
xmpp.on('stanza', stanza => {
  console.log(stanza.toString())
  if (!stanza.is('message')) return

  const message = stanza.clone()
  message.attrs.to = stanza.attrs.from
  xmpp.send(message)
})

Event online

Emitted when connected, authenticated and ready to receive/send stanzas.

xmpp.on('online', address => {
  console.log('online as', address.toString())
})

Event offline

Emitted when the connection is closed an no further attempt to reconnect will happen, usually after xmpp.stop().

xmpp.on('offline', () => {
  console.log('offline')
})

start

Starts the connection. Attempts to reconnect will automatically happen if it cannot connect or gets disconnected.

xmpp.start().catch(console.error)
xmpp.on('online', address => {
  console.log('online', address.toString())
})

Returns a promise that resolves if the first attempt succeed or rejects if the first attempt fails.

stop

Stops the connection and prevent any further auto reconnect/retry.

xmpp.stop().catch(console.error)
xmpp.on('offline', () => {
  console.log('offline')
})

Returns a promise that resolves once the stream closes and the socket disconnects.

send

Sends a stanza.

xmpp.send(xml('presence')).catch(console.error)

Returns a promise that resolves once the stanza is serialized and written to the socket or rejects if any of those fails.

xmpp.reconnect

See @xmpp-infister/reconnect.

Readme

Keywords

Package Sidebar

Install

npm i @xmpp-infister/component

Weekly Downloads

0

Version

0.9.3

License

ISC

Unpacked Size

8.31 kB

Total Files

5

Last publish

Collaborators

  • infister