3

At first, I was connecting to Openfire using following script.

const {Client} = require('@xmpp/client')
const client = new Client()
client.start('xmpp://localhost:5222').catch(err => {
    console.error('start failed', err)
})
client.handle('authenticate', authenticate => {
  return authenticate('gabbar', 'gabbar@123')
})

But it shows me an error 'require is not defined'. so I searched the internet and found that browserify could do my work. so I made the bundle.js file using index.js of my HTML page and included it in the HTML page.

<head>
   <meta charset="UTF-8"/>
   <title>xmpp.js example</title>
   <script src="bundle.js"></script>
  <!-- <script src="index.js"></script>-->
 </head>

but then I am getting the error

no compatible connection method found

Is anybody can tell any other way of doing it. I tried also same as given in example directory of xmpp.js client package, but that is giving me error like XMPP is not a function. Following is the code which I wrote after looking at example files.

index.js

const {xmpp, xml} =
    typeof require === 'undefined' ? window.xmpp : require('@xmpp/client') // For you; require('@xmpp/client')
const {client} = xmpp()
client.start('xmpp://localhost:5222').catch(err => {
    console.error('start failed', err)
})
client.handle('authenticate', authenticate => {
  return authenticate('gabbar', 'gabbar@123')
})

sample.html

<head>
    <meta charset="UTF-8"/>
    <title>xmpp.js example</title>
    <script src="node_modules/xmpp.js/dist/xmpp.min.js"></script>
    <script src="index.js"></script>    
  </head>

these are the two ways I tried connecting to openfire from the browser side but none of them worked for me. please, can anybody tell me what I am doing wrong or any other possible better way of doing this?

hssin
  • 55
  • 4

1 Answers1

0

xmpp:// is not supported in the browser. Only ws:// (websockets) is supported in browser. If the server supports websockets, you would do something like:

client.start('ws://domain:port) or client.start('ws://domain:port/xmpp-websockets)

The other option is to use Node not in a browser. Which would be accomplished by running node on it's own without a browser or running that code in the background process of Electron (same as just running node by itself, but you can communicate with the renderer process to interact with a UI)

Chris Hayes
  • 11,505
  • 6
  • 33
  • 41