Social Icons

Pages

Featured Posts

b

Why and where i should use .prototype in Javascript?


In JavaScript there is no concept of class instead you create an object. It's not difficult, but a little foreign for somebody used to the classical way.

function Person (name,age) {
    this.name = name;
    this.age = age;
    this.getInfo = function() {
        return this.name + ' is ' + this.age + ' years old.' ;
    };
}

var niki= new Person('Niki',30);
niki.age = 29; //want it younger?
alert(niki.getInfo());

In this example however, you will end up with a separate copy of the function for each and every instance of the class, which is simply wasted memory, since the function is identical in each instance. The best way to define methods for classes in this method is to define them outside of the constructor function itself, but as attributes of its prototype. This can be done fairly simply as follows, and solves both issues:
 
function Person (name,age) {
    this.name = name;
    this.age = age;
}
  
Person.prototype.getInfo = function() {
    return this.name + ' is ' + this.age + ' years old.' ;
};

Using this syntax changes nothing in the way you instantiate the object and use its properties and methods.
"Prototype" allows to simulate classes in JavaScript, because JavaScript's inheritance system is -prototypical, and not class-based.
Just think of constructor functions as classes and the properties of the prototype as shared members, ie members which are the same for each instance. In class-based systems, methods are implemented the same way for each instance, so methods are normally added to the prototype, whereas an object's fields are instance-specific and therefore added to the object itself during construction.

 

Freeswitch vs Opensips or where to use them

 Why is hard to find any comparisons infact?

You find no comparison because it's like comparing a bus to a car they both have wheels they both can provide transportation but they serve different purposes. Freeswitch is media server, Opensips is signalling proxy.
Ok but let's go deeper and see where we can use Freeswitch and where Opensips and where we could combine them and how, so we can get the best of them.

For what Opensips is good for?

It is designed to handle large volumes of calls, load balance SIP, solve advanced NAT scenarios, and to deal with SIP signalling and registrations as no other. OpenSIPS is typically used when you are managing large volumes such as more than a thousand registered users. Opensips can handle all the requests and responses. This makes it possible to translate between two incompatible versions of SIP, handling directly the SIP headers, requests, and responses. This is an important feature when you have SIP implementations from different manufacturers, which can be incompatible between each other. Above is especially important if your future system should handle with different voip vendors.

Tough it is not designed to handle natively audio, with the help of RTP proxy this can be easy overcome. It does really good job with NAT traversal (better than FS) especially the module responsible for that which is highly tweakable, allowing optimization of the media call flow between the endpoints.

OpenSIPS is not a platform that you would use to create systems such as IVR, VoiceMail, TTS, and Voice Recognition, etc. This is by design.

For what Freeswitch is good for?

As media server by design, for services as Voicemail,Conferencing, Text to speech, speech recognition FS is very valuable. Ability for easy configuration of complex call scenarios trough XML and navigating trough IVR menus as Auto Attendance or Voucher recharge is unique. But along with all that you can register users and proxy media.

In one sentence it can be used as a "black box does it all".

Then why to use Opensips when Freeswitch can do the same and more?

Some people think that just because Freeswitch has a lot of features and they completely overlap with opensips except Freeswitch have more, they decide to count entirely on Freeswitch and eventually use Opensips only for load balancer. But this is not really true, Freeswitch has a lot of features, but do you really need them in each call? No. That is why you should involve Freeswitch only when is necessary.

How best to combine them? What principles to follow?

Just because FS has really a lot of features while Opensips architecture is simpler you might guess that Opensips have better performance and stability.
Opensips has processing a light-weight instruction script and singe thread can process hundreds of calls per min. On the other side FS runs thread for each call, hooking up to the call and watch it closely all the time.

If you want performance and consistency you should build your system of components, which have good integration between them, and do the best for what are designed for.
With the swiss knife approach of FS you would end up with system which does its job but with more overhead than needed, less scalable, flexible with performance tradeoff.

Good theory but any real world examples?

In VVS design, Opensips is responsible for registration, authorizing the call, preforming dial plan translation rules, communicating with Billing engine to see if caller can actually call the destination and if it has enough money to make the call, check the type of dialed destination (onnet or offnet). If offnet will prepare list of appropriate GW's based on price and priority. If onnet will evaluate if the user is registered or not, if call should do Follow me policy or should be send to Freeswitch in order to trigger VM logic, Auto Attendance or play error message.  At the end, trough the help of Billing engine CDR is written, along with adjustments to the balance of vendor, reseller and customer.
This ofcourse is very basic flow.

All needed additional custom modules are written in C, natively plugged into the Opensips core.

All the logic can be deployed on one single instance so when you need more capacity you just deploy another one on the cloud, in matter of minutes with the help of automated script. On the front of these units there is another Opensips which acts as Load balancer, SBC and allows adding and removing of VVS units to become not noticed from end users.

In conclusion

 - for systems which will be used mostly as PBX you better deploy cluster of Freeswitch, load balanced by Opensips
 - for large scale deployments you should count on Opensips to do most of the tasks (along with its "helper" modules) and involve Freeswitch only if necessary. We should mention here Couchbase and MongoDB native support in next version 1.9 of Opensips. This will make clustering easy job with possibility to share DIALOG and USRLOC among all units and should help you to design piece of SIP architectural system of the future with unmatched scalability. Dreamy ah!?


Solving javascript console.log issue in IE



I recently run into strange issue. My interface work perfectly for all browsers except IE. My code was IE compatible and IE debugger did not show any errors, moreover when i enable the IE debugger everything works?!?! I thought world must hate me.
So i started asking questions myself and after hours of Microsoft swearing, changes and blind testing (could not use IE debugger) found all answers. Issue was related with console.log calls.

Why when i turn on IE debugger tools with F12 and refresh the page evryhting works?

In that case  "console" object exists in the the window name space

Ok so if i disable console.log then my interface will work on IE, but i really need to use console.log? 

Sure you could take out all your console.log statements, but usually you need those around for a little while.
You put this very simple snippet before all your console calls:
if (!window.console) console = {log: function() {}};
Thus your Firefox/Chrome console will still work and you won't get errors on Internet Explorer. With above for browsers which do not support console you create such a object so when you call it somewhere in your code browser will not complain.
Another JS mystery down..

Is JavaScript a pass-by-reference or pass-by-value language?

 

This really confuses a lot of people including me. Short answer is: It's always pass by value, even for object. There's no way to alter the value held by a variable passed as a parameter to function, which would be possible if JS supported passing by reference.

OK, why then when you pass object to function you can change it's members? 

Well for objects the value of the variable is a reference. Because of this, when you pass an object and change its members, those changes persist outside of the function. This makes it look like pass by reference. But if you actually change the value of the object variable you will see that the change does not persist, proving it's really pass by value.

Example:
function changeObject(x) {
  x = {member:"bar"};
  alert("in changeObject: " + x.member);
}
function changeMember(x) {
  x.member = "bar";
  alert("in changeMember: " + x.member);
}
var x = {member:"foo"};

alert("before changeObject: " + x.member);
changeObject(x);
alert("after changeObject: " + x.member); /* change did not persist */

alert("before changeMember: " + x.member);
changeMember(x);
alert("after changeMember: " + x.member); /* change persists */
Output:
before changeObject: foo
in changeObject: bar
after changeObject: foo

before changeMember: foo
in changeMember: bar
after changeMember: bar