Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@xmpp/component #338

Merged
merged 1 commit into from
Nov 19, 2016
Merged

@xmpp/component #338

merged 1 commit into from
Nov 19, 2016

Conversation

sonnyp
Copy link
Member

@sonnyp sonnyp commented Oct 16, 2016

brand new @xmpp/component modular package

Adds the following packages

  • @xmpp/connection
  • @xmpp/connection-tcp
  • @xmpp/component

The goal is to reuse those for client/server as it's already done in #309

Todo

  • now
    • tests
    • doc
    • fix FIXMEs and TODOs
  • later
    • @xmpp/component-core
    • @xmpp/reconnect
    • @xmpp/component-connection XEP-0225
    • @xmpp/component-protocol XEP-0114

and have @xmpp/component use them. Or maybe just files.

Please comment on the API as it would be the same for client and server.

'use strict'

const Component = require('@xmpp/component')
const entity = new Component()

// emitted for any error
entity.once('error', (err) => {
  console.log('error', err)
})

// emitted for incoming stanza _only_ (iq/presence/message) qualified with the right namespace
entity.on('stanza', (stanza) => {
  console.log('stanza', stanza.toString())
})

// emitted for incoming nonza _only_
entity.on('nonza', (nonza) => {
  console.log('nonza', nonza.toString())
})

// emitted for any in our out XML fragment
entity.on('fragment', (output, input) => {
  console.log(output ? '=>' : '<=', (output || input).trim())
})

// "start" opens the socket and the XML stream
entity.start('xmpp:component.localhost:5347')
  // resolves once online
  .then((jid) => {
    console.log('started', jid.toString())
  })
  // rejects for any error before online
  .catch(err => {
    console.error(err)
  })

// emitted when the socket is open
entity.on('connect', () => {
  console.log('connected')
})

// emitted when the XMPP stream has open and we received the server stream
entity.on('open', (el) => {
  console.log('open')
})

// emitted when authentication is required
entity.on('authenticate', authenticate => {
  authenticate('foobar')
    .then(() => {
      console.log('authenticated')
    })
    .catch((err) => {
      console.error('authentication failed', err)
    })
})

// emitted when the XMPP entity is ready and has a jid
entity.on('online', (jid) => {
  console.log('online', jid.toString())
})

process.on('unhandledRejection', function (reason, p) {
  console.log('Possibly Unhandled Rejection at: Promise ', p, ' reason: ', reason)
})

Here is an example with async/await

const jid = await entity.start('xmpp:component.localhost:5347')
entity.send(...)

@sonnyp sonnyp mentioned this pull request Nov 4, 2016
@sonnyp sonnyp mentioned this pull request Nov 6, 2016
@sonnyp sonnyp force-pushed the @xmpp/component branch 5 times, most recently from a5b7e21 to 07f2403 Compare November 19, 2016 03:19
@sonnyp sonnyp merged commit fe36dd4 into master Nov 19, 2016
@sonnyp sonnyp deleted the @xmpp/component branch November 19, 2016 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant