I'm running around in circles trying to get sip.js running in nodejs environment.
So I went back to first principles. By which I mean I wrote a simple web app that uses sip.js (version 0.21.2). In that environment, I have the following code sequence:
var userAgent = new SIP.UserAgent({
transportOptions: {
},
authorizationUsername: 'testharness',
authorizationPassword: "wRvCZDZU2MBGu2Wo",
userAgentString: 'Solink frontend testing with sip.js 20.2',
})
// Now start up SIP client
let registerer
userAgent.start()
.then(() => {
When I invoke the 'start' method all works well and I fall into the 'then()' handler. I subsequently then add listeners for events and subsequently register - and received the 'Registered' event. In short, this is working entirely as expected.
However, things fall apart in nodejs. Specifically I'm running node v18.16.1. Again, I am using the sip.js version 0.21.2
When I run the very similar code sequence:
this.sipPhone = new SIP.UserAgent({
transportOptions: {
},
authorizationUsername: 'testharness',
authorizationPassword: "xxxxxxxxx",
userAgentString: 'Solink frontend testing with sip.js 20.2',
})
let registerer
this.sipPhone.start()
.then(() => {
I never get to the 'then()' handler. Rather an exception is thrown. Here are the console logs that are created.
Fri Oct 20 2023 08:12:58 GMT-0400 (Eastern Daylight Time) | sip.UserAgent | Starting sip:
solin...@testharness.onsip.comFri Oct 20 2023 08:12:58 GMT-0400 (Eastern Daylight Time) | sip.UserAgent | Transitioned from Stopped to Started
Fri Oct 20 2023 08:12:58 GMT-0400 (Eastern Daylight Time) | sip.Transport | Connecting wss://
edge.sip.onsip.comFri Oct 20 2023 08:12:58 GMT-0400 (Eastern Daylight Time) | sip.Transport | Transitioned from Disconnected to Connecting
(node:10925) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Fri Oct 20 2023 08:12:58 GMT-0400 (Eastern Daylight Time) | sip.Transport | WebSocket error occurred.
Fri Oct 20 2023 08:12:58 GMT-0400 (Eastern Daylight Time) | sip.Transport | WebSocket closed unexpectedly
Fri Oct 20 2023 08:12:58 GMT-0400 (Eastern Daylight Time) | sip.Transport | WebSocket closed wss://
edge.sip.onsip.com (code: 1006)
Fri Oct 20 2023 08:12:58 GMT-0400 (Eastern Daylight Time) | sip.Transport | Transitioned from Connecting to Disconnected
I'm at a loss how to proceed. I'm told sip.js runs nicely in a nodejs environment. But I'm perplexed as to what is happening now and how to proceed.
Initially I had the latest version of npm package 'ws' installed (v8.14.2). I tried downshifting to version 7.5.9. However, the result is identical.
Also maybe of note. This nodejs app I'm writing is using commonJS. I've had to massage things slightly in order to be able to 'import' the sip.js file (all my other code uses 'require' and not 'import').
I could also say I tried an earlier version of sip.js - version 0.20.1. This presented other issues - specifically because node crashes with the message - ' Cannot find module '/share/CACHEDEV1_DATA/homes/admin/analytics/node_modules/sip.js/lib/version' imported from /share/CACHEDEV1_DATA/homes/admin/analytics/node_modules/sip.js/lib/index.js'. I didn't bother trying to figure out what that problem was and reverted to the latest sip.js package.
Finally, I tried this simple chunk of code
const WebSocket = require('ws');
// Create a WebSocket instance and provide the URI
const ws = new WebSocket(uri);
// Event handler for when the connection is established
ws.on('open', () => {
console.log('WebSocket connection is open.');
});
ws.on('error', (error) => {
console.error(`WebSocket error: ${error}`);
});
// Event handler for connection closure
ws.on('close', () => {
console.log('WebSocket connection is closed.');
});
When I run it, I receive the 'error' event with the message 'Unexpected server response: 400'. I'm guessing this is the problem that the sip.js package is encountering.
4. I installed the npm package - wscat. Once installed and I run the command: wscat -c wss://
edge.sip.onsip.com, I get the error - 'error: Unexpected server response: 400'
I'm way out of my comfort zone now in speculating that I need some sort of authentication on this request? Any feedback on that hypothesis or some other ideas?
Has anyone else had any success running sip.js in a nodejs environment? Does anyone have any suggestions for me to get past this difficulty? To install, I just used npm i sip.js. After installing, is there some script I need to run perhaps? Should I pull it directly from github???
Thanks in advance
Jim