{"id":15585856,"url":"https://github.com/kdcro101/xmpp-bosh-client","last_synced_at":"2025-04-24T04:19:45.742Z","repository":{"id":57401991,"uuid":"137733085","full_name":"kdcro101/xmpp-bosh-client","owner":"kdcro101","description":"XMPP BOSH protocol client for JavaScript/Typescript. (Re)Written in typescript","archived":false,"fork":false,"pushed_at":"2019-04-05T08:03:10.000Z","size":609,"stargazers_count":16,"open_issues_count":6,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T22:06:55.019Z","etag":null,"topics":["javascript","node","typescript","web-worker","xmpp"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kdcro101.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-18T09:28:32.000Z","updated_at":"2024-02-17T00:28:19.000Z","dependencies_parsed_at":"2022-09-15T18:40:39.530Z","dependency_job_id":null,"html_url":"https://github.com/kdcro101/xmpp-bosh-client","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdcro101%2Fxmpp-bosh-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdcro101%2Fxmpp-bosh-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdcro101%2Fxmpp-bosh-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kdcro101%2Fxmpp-bosh-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kdcro101","download_url":"https://codeload.github.com/kdcro101/xmpp-bosh-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250309194,"owners_count":21409407,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","node","typescript","web-worker","xmpp"],"created_at":"2024-10-02T21:04:16.084Z","updated_at":"2025-04-24T04:19:45.722Z","avatar_url":"https://github.com/kdcro101.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# xmpp-bosh-client\n\nXMPP [BOSH](https://en.wikipedia.org/wiki/BOSH_(protocol)) client for Javascript/Typescript.\n\n\nJump to module [interface](#interface)\n\n## Features\n\n- works in browser\n- works in Web worker\n- strictly typed\n- includes implementation of [XEP-0199: XMPP Ping](https://xmpp.org/extensions/xep-0199.html)\n\n\n## Installation\n\n`npm install xmpp-bosh-client`\n\n## Usage\n\n1) Import BoshClient\n\nWhen using with node.js\n\n```js\nimport { BoshClient, $build } from \"xmpp-bosh-client/node\";\n```\nWhen using with typescript framework running in browser (angular/react/etc)\n```\nimport { BoshClient, $build } from \"xmpp-bosh-client/browser\";\n```\n\n2) construct BoshClient object\n\n```js\nconst connection = new BoshClient(USERNAME, PASSWORD, URL);\n```\n\n3) setup event listeners\n\n```js\n connection.on(\"error\", errorListener);\n connection.on(\"stanza\", stanzaListener);\n connection.on(\"online\", onlineListener);\n connection.on(\"offline\", offlineListener);\n```\n\n4) start connecting procedure\n\n```js\nconnection.connect()\n```\n\n\n### Typescript\n```ts\n// when using with Node.js\nimport { BoshClient } from \"xmpp-bosh-client/node\"; \n// when using with angular/react (execution in browser)\nimport { BoshClient } from \"xmpp-bosh-client/browser\"; \n\nconst USERNAME = \"username@example.com\";\nconst PASSWORD = \"somePassword\";\nconst URL = \"https://www.example.com:5280/http-bind/\";\n\n    const client = new BoshClient(USERNAME, PASSWORD, URL);\n\n    client.on(\"error\", (e) =\u003e {\n        console.log(\"Error event\");\n        console.log(e);\n    });\n    client.on(\"online\", () =\u003e {\n        console.log(\"Connected successfully\");\n    });\n    \n    client.on(\"ping\", () =\u003e {\n        console.log(`Ping received at ${new Date()}`);\n    });\n    \n    client.on(\"stanza\", (stanza) =\u003e {\n        console.log(`Stanza received at ${new Date()}`);\n        console.log(stanza);\n    });\n\n    client.on(\"offline\", () =\u003e {\n        console.log(\"Disconnected/Offline\");\n    });\n\n    connection.connect();\n\n```\n\n### Javascript\n```js\nvar lib = require(\"xmpp-bosh-client/node\");\n// when using with Node.js\nvar lib = require(\"xmpp-bosh-client/browser\");\n// when using with angular/react (execution in browser)\n\nvar USERNAME = \"username@example.com\";\nvar PASSWORD = \"somePassword\";\nvar URL = \"https://www.example.com:5280/http-bind/\";\n\n    var client = new lib.BoshClient(USERNAME, PASSWORD, URL);\n    client.on(\"error\", function (e) {\n        console.log(\"Error event\");\n        console.log(e);\n    });\n    client.on(\"online\", function () {\n        console.log(\"Connected successfully\");\n    });\n    client.on(\"ping\", function () {\n        console.log(\"Ping received at \" + new Date());\n    });\n    client.on(\"stanza\", function (stanza) {\n        console.log(\"Stanza received at %s\",new Date());\n        console.log(stanza);\n    });\n    client.on(\"offline\", function () {\n        console.log(\"Disconnected/Offline\");\n    });\n    \n    client.connect();\n\n```\n\n### Browser (classic)\n\nInclude script tag, for example:\n\n```html\n\u003cscript src=\"./node_modules/xmpp-bosh-client/browser-bundle/index.js\"\u003e\u003c/script\u003e\n```\n\nexports will be accessible via `BoshXMPP` wrapper:\n\n```js\n    var client =  new BoshXMPP.BoshClient(USERNAME, PASSWORD, URL);\n    \n    client.on(\"error\", (e) =\u003e {\n        console.log(\"Error event\");\n        console.log(e);\n    });\n    client.on(\"online\", () =\u003e {\n        console.log(\"Connected successfully\");\n    });\n    \n    client.on(\"ping\", () =\u003e {\n        console.log(`Ping received at ${new Date()}`);\n    });\n    \n    client.on(\"stanza\", (stanza) =\u003e {\n        console.log(`Stanza received at ${new Date()}`);\n        console.log(stanza);\n    });\n\n    client.on(\"offline\", () =\u003e {\n        console.log(\"Disconnected/Offline\");\n    });\n\n    connection.connect();\n\n```\n\nCopy `index.js` file in location of your convenience and update src attribute.\n\n\n \n### Browser (angular or other typescript based framwork)\n\nSee typescript example above.\n\n# Stanza building\n```js\n    const root: XmlElement = $build('message', { to: \"username@example.com\" });\n    const child1 = root.cnode($build(\"header\", {\n        id: \"123\",\n        jid: \"user@example.com\"\n    }));\n    child1.cnode($build(\"some-element\", {\n        a: \"1\",\n        b: 2\n    }));\n```\nWould generate:\n```xml\n\u003cmessage to=\"username@example.com\"\u003e\n        \u003cheader id=\"123\" jid=\"user@example.com\"\u003e\n            \u003csome-element a=\"1\" b=\"2\"/\u003e\n        \u003c/header\u003e\n        \u003cbody\u003e\n            some inner text\n        \u003c/body\u003e\n\u003c/message\u003e\n\n```\n\n\n# \u003ca name=\"interface\"\u003e\u003c/a\u003e Interface\n\n## Constructor(jid, password, boshUrl, route)\nConstructs BoshClient instance\n```\njid      [string] : XMPP username to connect with\npassword [string] : password to connect with\nboshUrl  [string] : URL to connect to (example: https://www.example.com:5280/http-bind/)\nroute    [string] : optional. routing server for connection. see https://xmpp.org/extensions/xep-0124.html#session-request\n```\n## on(event_name, listener)\nRegister event listener\n```\nevent_name [string]   : event name. One of: online,offline,stanza,error,ping\nlistener   [function] : event listener function\n\nData type for event callbacks:\n\nonline   -\u003e void\noffline  -\u003e string\nerror    -\u003e string\nstanza   -\u003e XmlElement\nping     -\u003e XmlElement\n\n```\n\n## off(event_name, listener)\nUnregister event listener\n```\nevent_name [string]   : event name. One of: online,offline,stanza,error,ping\nlistener   [function] : event listener function\n```\n\n## connect()\nStart connecting procedure\n\n## send(stanza)\nSends XML stanza to server\n```\nstanza [XmlElement] : Stanza to send\n```\n## sendMessage(to, mbody, type)\nSends chat message \n```\nto    [string] : destination XMPP username (user@domain)\nmbody [string] : Message body\ntype  [string] : optional. type attribute, defaults to \"chat\"\n```\n## disconnect()\nSends any pending stanzas and terminates connection.\n\n## unregisterListeners()\nUnregister all registred listeners. Useful when you don't want to trigger any events after disconnect.\n\n## errors\n- *auth_error* :  invalid credentials. Error while authenticating\n- *xml_parsing_error* :  error parsing incoming stanza string \n- *binding_error* : error while binding to resource\n- *session_create_error* : error while creating session\n- *start_sasl_error* : no sasl mechanism available\n- *plain_sasl_unavailable_error*: on plain sasl mechanism available\n\n\n## ltxElement\n\nReference to ltx.Element constructor. See [this](https://github.com/xmppjs/ltx).\nUse to construct XML element.\n\nreturns `XmlElement`\n\n```js\n const e = new ltxElement(\"element\",{\n    attr1: \"some_value\",\n    attr2: \"some_other_value\"\n}) \n```\n\n## $build(name, attrs)\nalias for `new ltxElement(name, attrs)`\n\nreturns `XmlElement`\n\n```js\n const e = $build(\"element\",{\n    attr1: \"some_value\",\n    attr2: \"some_other_value\"\n}) \n```\n\n## $msg(attrs)\nHelper to construct message stanza. Alias for `$build(\"message\",attrs)`\n\nreturns `XmlElement`\n\n## $iq(attrs)\nHelper to construct `iq` stanza. Alias for `$build(\"iq\",attrs)`\n\nreturns `XmlElement`\n\n## $pres(attrs)\nHelper to construct `presence` stanza. Alias for `$build(\"presence\",attrs)`\n\nreturns `XmlElement`\n\n\n# Additional reading\n\nRead [this](https://metajack.im/2008/07/02/xmpp-is-better-with-bosh/) article.\n\n# Credits \n\nThanks to https://github.com/eelcocramer and his [work](https://github.com/eelcocramer/node-bosh-xmpp-client)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkdcro101%2Fxmpp-bosh-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkdcro101%2Fxmpp-bosh-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkdcro101%2Fxmpp-bosh-client/lists"}