Sip.js - latest version running in nodejs

596 views
Skip to first unread message

Jim Udall

unread,
Oct 21, 2023, 10:02:43 AM10/21/23
to SIP.js
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({
        uri: SIP.UserAgent.makeURI("sip:solin...@testharness.onsip.com"),
        transportOptions: {
            server: "wss://edge.sip.onsip.com",
        },
        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({
                uri: SIP.UserAgent.makeURI("sip:ss...@testharness.onsip.com"),
                transportOptions: {
                    server: "wss://edge.sip.onsip.com",
                },
                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.com
Fri 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.com
Fri 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');

const uri = 'wss://edge.sip.onsip.com';

// 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

Henrique Souza

unread,
Oct 23, 2023, 1:56:35 PM10/23/23
to sip...@googlegroups.com
Hi, I'm using this library in Node, can I help you with something?

Em sáb., 21 de out. de 2023 às 11:02, Jim Udall
<jim.u...@gmail.com> escreveu:
> --
> You received this message because you are subscribed to the Google Groups "SIP.js" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sip_js+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sip_js/dbb4d4de-9025-4e7a-ae2c-88c2e605cd15n%40googlegroups.com.

Jim Udall

unread,
Nov 15, 2023, 12:03:40 PM11/15/23
to SIP.js
So I've discovered the fundamental problem here.  Specifically there is a difference between what headers are included in the creation of the websocket running in a browser and that running in a nodejs environment.  Specifically, it seems the WebSocket server running at wss://edge.sip.onsip.com is looking for a valid 'origin' header.  In the nodejs implementation of websockets (via 'ws' package), there is NO 'origin' header applied.  And the ONSIP websocket server rejects it.

I find this piece of information fundamentally valuable and contraindicates that sip.js works in a nodejs environment.  Specifically, the following information should be clearly identified if trying to run sip.js in a nodejs environment:
1.  Prior to using the sip.js package ENSURE YOU SET A GLOBAL VARIABLE - 'WebSocket' to a websocket package - such as global.WebSocket = require('ws')
2.  You SHOULD ensure there is an 'origin' header in the request - and particularly for the ONSIP websocket server.  Specifically code such as this is required:
const OriginalWebSocket = require('ws')
class WebSocket extends OriginalWebSocket {
    constructor(address, protocols, options) {
        // Add or modify the Origin header
        if (!options)
            options = {}
        options.headers = options.headers || {};
        options.headers['Origin'] = options.headers['Origin'] || 'http://example.com'

        // Call the parent constructor
        super(address, protocols, options)
    }
}
global.WebSocket = WebSocket


Admittedly, not ALL websocket servers may require the 'origin' header.  However, given that sip.js comes from ONSIP and the example code uses the ONSIP websocket server, then this would be VALUABLE information to know 
Reply all
Reply to author
Forward
Message has been deleted
0 new messages