SIP Protocol Integration¶
Session Initiation Protocol (SIP) is the foundation of modern VoIP and multimedia communication services.
Overview¶
SIP enables:
- Voice and video calls
- Instant messaging
- Presence information
- Multimedia sessions
- WebRTC integration
Basic Setup¶
const sipConfig = {
domain: 'sip.example.com',
port: 5060,
transport: ['udp', 'tcp', 'tls'],
userAgent: 'TelcoAPI-SIP/1.0'
};
const client = await telcoapi.sip.createClient(sipConfig);
Registration¶
User Registration¶
const registration = {
username: 'alice',
password: 'secret',
realm: 'example.com',
expires: 3600
};
await client.sip.register(registration);
Multiple Accounts¶
const accounts = [
{
username: 'support',
password: 'pass1',
realm: 'support.example.com'
},
{
username: 'sales',
password: 'pass2',
realm: 'sales.example.com'
}
];
await client.sip.registerMultiple(accounts);
Call Handling¶
Making Calls¶
// Basic call
const call = await client.sip.call({
to: 'bob@example.com',
from: 'alice@example.com',
mediaOptions: {
audio: true,
video: false
}
});
// Handle call events
call.on('ringing', () => {
console.log('Call is ringing');
});
call.on('connected', () => {
console.log('Call connected');
});
Receiving Calls¶
client.sip.on('incomingCall', async (call) => {
console.log(`Incoming call from ${call.from}`);
// Auto-answer example
if (call.from.includes('priority')) {
await call.answer({
audio: true,
video: call.hasVideo()
});
} else {
await call.reject(486); // Busy Here
}
});
Media Handling¶
Audio Configuration¶
const audioConfig = {
codecs: ['OPUS', 'G.711', 'G.729'],
echoCancellation: true,
noiseSuppression: true,
autoGainControl: true
};
await client.sip.setAudioConfig(audioConfig);
Video Configuration¶
const videoConfig = {
codecs: ['VP8', 'H.264'],
resolution: '720p',
frameRate: 30,
bandwidth: 1500 // kbps
};
await client.sip.setVideoConfig(videoConfig);
Messaging¶
Instant Messages¶
// Send message
await client.sip.sendMessage({
to: 'bob@example.com',
content: 'Hello, Bob!',
contentType: 'text/plain'
});
// Receive messages
client.sip.on('message', (msg) => {
console.log(`Message from ${msg.from}: ${msg.content}`);
});
Message with Media¶
await client.sip.sendMessage({
to: 'bob@example.com',
content: '<image>base64...</image>',
contentType: 'application/xml+media'
});
Presence¶
Publishing Status¶
Subscribing to Presence¶
const subscription = await client.sip.subscribe({
to: 'bob@example.com',
event: 'presence',
expires: 3600
});
subscription.on('notify', (presence) => {
console.log(`Bob's status: ${presence.status}`);
});
Security¶
TLS Configuration¶
const tlsConfig = {
cert: fs.readFileSync('client-cert.pem'),
key: fs.readFileSync('client-key.pem'),
ca: fs.readFileSync('ca-cert.pem')
};
await client.sip.setTLSConfig(tlsConfig);
SRTP Configuration¶
const srtpConfig = {
profiles: ['AEAD_AES_256_GCM', 'AES_CM_128_HMAC_SHA1_80'],
requireEncryption: true
};
await client.sip.setSRTPConfig(srtpConfig);
WebRTC Integration¶
const webrtcConfig = {
iceServers: [
{ urls: 'stun:stun.example.com:3478' },
{
urls: 'turn:turn.example.com:3478',
username: 'user',
credential: 'pass'
}
]
};
const webrtcClient = await client.sip.createWebRTCClient(webrtcConfig);
Monitoring¶
Call Quality Metrics¶
call.on('stats', (stats) => {
console.log('Packet Loss:', stats.packetLoss);
console.log('Jitter:', stats.jitter);
console.log('Round Trip Time:', stats.rtt);
});
SIP Traces¶
client.sip.on('trace', (message) => {
console.log(`${message.direction} ${message.method}`);
console.log(message.headers);
});
Best Practices¶
-
Connection Management
-
Call Recovery
-
Resource Cleanup