XMPP For The Web/Win ::: XMPP-FTW (1.16.8)

Publishing to PubSub Nodes

Publishing to PubSub Nodes (XEP-0060)

Publishing (and indeed reading) data to pubsub nodes is handled by the item parser code. At present this is part of XMPP-FTW but may in future be split out for the use of others.

The base folder for the item parser code lives in its own NPM module: xmpp-ftw-item-parser. This is because the conversion between XML and JSON may be useful for projects outside of XMPP.

The code is new and therefore may change until a suitable format is found.

What are we trying to achieve?

The item parser, like XMPP-FTW itself, is not designed to be a generic XML↔JSON parser. Its aim is to take a known subset of popular data formats and enable the buidling of valid XML payloads, and conversely taking valid XML data payloads and translating these into sensible JSON.

In the short term I foresee the support of ATOM payloads and a couple of the popular extensions, e.g. Activity streams.

Note: While building XML payloads the parser should try and fill in any missing required data with suitable values but it is not designed to check that the data provided builds a standards compliant payload.

How it works

XML→JSON

A range of parsers are called in a loop each adding to the JSON object as appropriate if the format suits.

Most of the parsers are able to detect the presence of their XMLNS and can exit quickly if its parsing is not appropriate for the payload.

JSON→XML

Building valid XML payloads is slightly more complex than translating XML data formats to JSON. The current methodology is to use an associative array to tell the parser that it has work to do for the passed JSON object. As usual the best description is via an example so here we'll do a minimal ATOM post.

    {
        atom: {
            content: {
                content: '<p>Where we're going we don't need roads!</p>',
                lang: 'en_GB',
                base: 'http://doc.brown.org',
                type: 'xhtml'
            },
            title: 'Back to the future quote of the day',
            published: '2014-01-13T13:19:00.000Z',
            author: {
                name: 'Doc Brown'
            }
        },
        'in-reply-to': {
            ref: 'item-12345-parent'
        },
        activity: {
            target: {
                id: '12345',
                'object-type': 'comment'
            },
            verb: 'rated',
            object: {
                'object-type': 'comment'
            },
            author: {
                'object-type': 'person'
            }
        },
        review: {
            rating: '5.0'
        }
    }

Which would build the following XML payload:

    <item>
        <entry xmlns="http://www.w3.org/2005/Atom"
            xmlns:thr="http://purl.org/syndication/thread/1.0"
            xmlns:activity="http://activitystrea.ms/spec/1.0/"
            xmlns:review="http://activitystrea.ms/schema/1.0/review">
            <content xml:lang="en_GB" xml:base="http://doc.brown.org" type="xhtml">
                <p>Where we\re going we don\t need roads!</p>
            </content>
            <title>Back to the future quote of the day</title>
            <published>2014-01-13T13:19:00.000Z</published>
            <author>
                <name>Doc Brown</name>
                <activity:object-type>
                    person
                </activity:object-type>
            </author>
            <thr:in-reply-to ref="item-12345-parent"/>
            <activity:target>
                <id>12345</id>
                <object-type>comment</object-type>
            </activity:target>
            <activity:verb>rated
                </activity:verb>
            <activity:object>
                <activity:object-type>comment
                    </activity:object-type>
            </activity:object>
            <review:rating>5.0
                </review:rating>
        </entry>
    </item>

In the above JSON payload the user was able to omit the updated date and the item id as these can be generated by the parser.

The mapping between JSON and XML is completely reversible therefore submitting the XML stanza to the parser would return the original JSON payload.

Pages

Fork me on GitHub