7 Copyright © 2012-2013 Crocodile RCS Ltd
8 __________________________________________________________________
17 2.1. Initiating a connection
18 2.2. SIP message routing
19 2.3. MSRP message routing
24 3.2. External Libraries or Applications
28 4.1. keepalive_mechanism (integer)
29 4.2. keepalive_timeout (integer)
30 4.3. keepalive_processes (integer)
31 4.4. keepalive_interval (integer)
32 4.5. ping_application_data (string)
33 4.6. sub_protocols (integer)
34 4.7. cors_mode (integer)
35 4.8. verbose_list (int)
39 5.1. ws_handle_handshake()
40 5.2. ws_close([status, reason[, connection_id]])
55 8. Exported Pseudo Variables
61 1.1. event_route[xhttp:request]
62 1.2. WebSocket SIP Routing
63 1.3. Set keepalive_mechanism parameter
64 1.4. Set keepalive_timeout parameter
65 1.5. Set keepalive_processes parameter
66 1.6. Set keepalive_interval parameter
67 1.7. Set ping_application_data parameter
68 1.8. Set sub_protocols parameter
69 1.9. Set cors_mode parameter
70 1.10. Set verbose_list parameter
71 1.11. ws_handle_handshake usage
73 1.13. event_route[websocket:closed] usage
76 Chapter 1. Admin Guide
83 2.1. Initiating a connection
84 2.2. SIP message routing
85 2.3. MSRP message routing
90 3.2. External Libraries or Applications
94 4.1. keepalive_mechanism (integer)
95 4.2. keepalive_timeout (integer)
96 4.3. keepalive_processes (integer)
97 4.4. keepalive_interval (integer)
98 4.5. ping_application_data (string)
99 4.6. sub_protocols (integer)
100 4.7. cors_mode (integer)
101 4.8. verbose_list (int)
105 5.1. ws_handle_handshake()
106 5.2. ws_close([status, reason[, connection_id]])
119 7.1. websocket:closed
121 8. Exported Pseudo Variables
127 This module implements a WebSocket (RFC 6455) server and provides
128 connection establishment (handshaking), management (including
129 connection keep-alive), and framing for the SIP and MSRP WebSocket
130 sub-protocols (RFC 7118 and RFC 7977).
132 The module supports WebSockets (ws) and secure WebSockets (wss)
137 2.1. Initiating a connection
138 2.2. SIP message routing
139 2.3. MSRP message routing
141 2.1. Initiating a connection
143 A WebSocket connection is initiated with an HTTP GET. The xhttp module
144 is used to handle this GET and call the Section 5.1, “
145 ws_handle_handshake() ” exported function.
147 event_route[xhttp:request] should perform some validation of the HTTP
148 headers before calling Section 5.1, “ ws_handle_handshake() ”. The
149 event_route can also be used to make sure the HTTP GET has the correct
150 URI, perform HTTP authentication on the WebSocket connection, and check
151 the Origin header (RFC 6454) to ensure a browser-based SIP UA or MSRP
152 client has been downloaded from the correct location.
154 Example 1.1. event_route[xhttp:request]
157 loadmodule "xhttp.so"
158 loadmodule "msrp.so" # Only required if using MSRP over WebSockets
159 loadmodule "websocket.so"
161 event_route[xhttp:request] {
163 set_reply_no_connect();
171 xlog("L_WARN", "HTTP request received on $Rp\n");
172 xhttp_reply("403", "Forbidden", "", "");
176 xlog("L_DBG", "HTTP Request Received\n");
178 if ($hdr(Upgrade)=~"websocket"
179 && $hdr(Connection)=~"Upgrade"
182 # Validate Host - make sure the client is using the correct
183 # alias for WebSockets
184 if ($hdr(Host) == $null || !is_myself("sip:" + $hdr(Host))) {
185 xlog("L_WARN", "Bad host $hdr(Host)\n");
186 xhttp_reply("403", "Forbidden", "", "");
190 # Optional... validate Origin - make sure the client is from an
191 # authorised website. For example,
193 # if ($hdr(Origin) != "http://communicator.MY_DOMAIN"
194 # && $hdr(Origin) != "https://communicator.MY_DOMAIN") {
195 # xlog("L_WARN", "Unauthorised client $hdr(Origin)\n");
196 # xhttp_reply("403", "Forbidden", "", "");
200 # Optional... perform HTTP authentication
202 # ws_handle_handshake() exits (no further configuration file
203 # processing of the request) when complete.
204 if (ws_handle_handshake())
206 # Optional... cache some information about the
207 # successful connection
212 xhttp_reply("404", "Not found", "", "");
216 2.2. SIP message routing
218 SIP over WebSockets uses invalid URIs in routing headers (Contact:,
219 Record-Route:, and Via:) because a JavaScript stack running in a
220 browser has no way to determine the local address from which the
221 WebSocket connection is made. This means that the routing headers
222 cannot be used for request or response routing in the normal manner.
224 RFC 7118 - The WebSocket Protocol as a Transport for the Session
225 Initiation Protocol - states that SIP WebSocket Clients and the SIP
226 registrar should implement SIP Outbound (RFC 5626) and Path header
227 support (RFC 3327) to enable requests and responses to be correctly
228 routed. Kamailio has a module called "Outbound" for this functionality.
230 The nathelper module functions (nat_uac_test(), fix_nated_register(),
231 add_contact_alias(), and handle_ruri_alias()) and the Kamailio core
232 force_rport() can be used to ensure correct routing of SIP WebSocket
233 requests without using Outbound and Path.
235 Example 1.2. WebSocket SIP Routing
240 loadmodule "websocket.so"
244 # per request initial checks
247 if (nat_uac_test(64)) {
248 # Do NAT traversal stuff for requests from a WebSocket
249 # connection - even if it is not behind a NAT!
250 # This won't be needed in the future if Kamailio and the
251 # WebSocket client support Outbound and Path.
253 if (is_method("REGISTER"))
254 fix_nated_register();
256 if (!add_contact_alias()) {
257 xlog("L_ERR", "Error aliasing contact <$ct>\n");
258 sl_send_reply("400", "Bad Request");
264 if (!is_method("REGISTER"))
265 t_on_reply("WS_REPLY");
267 # Handle requests within SIP dialogs
270 # sequential request withing a dialog should
271 # take the path determined by record-routing
274 if (!handle_ruri_alias()) {
275 xlog("L_ERR", "Bad alias <$ru>\n");
276 sl_send_reply("400", "Bad Request");
282 if ( is_method("ACK") ) {
284 onreply_route[WS_REPLY] {
285 if (nat_uac_test(64)) {
286 # Do NAT traversal stuff for replies to a WebSocket connection
287 # - even if it is not behind a NAT!
288 # This won't be needed in the future if Kamailio and the
289 # WebSocket client support Outbound and Path.
295 2.3. MSRP message routing
297 MSRP over WebSocket clients create invalid local URIs for use in Path
298 headers (From-Path: and To-Path:) because a JavaScript stack running in
299 a browser has no way to determine the local address from which the
300 WebSocket connection is made. This is OK because MSRP over WebSocket
301 clients MUST use an MSRP relay and it is the MSRP relay's
302 responsibility to select the correct connection to the client based on
303 the MSRP URIs that it has created (and maintains a mapping for).
307 3.1. Kamailio Modules
308 3.2. External Libraries or Applications
310 3.1. Kamailio Modules
312 The following module must be loaded before this module:
316 The following modules are required to make proper use of this module:
317 * nathelper or outbound.
320 The following module is required to use the secure WebSocket (wss)
324 The following module is required to support MSRP over WebSockets:
327 3.2. External Libraries or Applications
329 The following libraries must be installed before running Kamailio with
336 4.1. keepalive_mechanism (integer)
337 4.2. keepalive_timeout (integer)
338 4.3. keepalive_processes (integer)
339 4.4. keepalive_interval (integer)
340 4.5. ping_application_data (string)
341 4.6. sub_protocols (integer)
342 4.7. cors_mode (integer)
343 4.8. verbose_list (int)
345 4.1. keepalive_mechanism (integer)
347 The keep-alive mechanism to use for WebSocket connections.
351 If nathelper is only being used for WebSocket connections then
352 nathelper NAT pinging is not required. If nathelper is used for
353 WebSocket connections and TCP/TLS aliasing/NAT-traversal then WebSocket
354 keep-alives are not required.
356 * 0 - no WebSocket keep-alives
357 * 1 - Ping WebSocket keep-alives
358 * 2 - Pong WebSocket keep-alives
362 Example 1.3. Set keepalive_mechanism parameter
364 modparam("websocket", "keepalive_mechanism", 0)
367 4.2. keepalive_timeout (integer)
369 The time (in seconds) after which to send a keep-alive on idle
370 WebSocket connections.
372 Default value is 180.
374 Example 1.4. Set keepalive_timeout parameter
376 modparam("websocket", "keepalive_timeout", 30)
379 4.3. keepalive_processes (integer)
381 The number of processes to start to perform WebSocket connection
386 Example 1.5. Set keepalive_processes parameter
388 modparam("websocket", "keepalive_processes", 2)
391 4.4. keepalive_interval (integer)
393 The number of seconds between each keep-alive process run
397 Example 1.6. Set keepalive_interval parameter
399 modparam("websocket", "keepalive_interval", 2)
402 4.5. ping_application_data (string)
404 The application data to use in keep-alive Ping and Pong frames.
406 Default value is Kamailio Server: header content
408 Example 1.7. Set ping_application_data parameter
410 modparam("websocket", "ping_application_data", "WebSockets rock")
413 4.6. sub_protocols (integer)
415 A bitmap that allows you to control the sub-protocols supported by the
418 * 2 - msrp (RFC 7977) - msrp.so must be loaded before websocket.so
420 Default value is 1 when msrp.so is not loaded 3 when msrp.so is loaded.
422 Example 1.8. Set sub_protocols parameter
424 modparam("websocket", "sub_protocols", 2)
427 4.7. cors_mode (integer)
429 This parameter lets you set the "Cross-origin resource sharing"
430 behaviour of the WebSocket server.
431 * 0 - Do not add an "Access-Control-Allow-Origin:" header to the
432 response accepting the WebSocket handshake.
433 * 1 - Add a "Access-Control-Allow-Origin: *" header to the response
434 accepting the WebSocket handshake.
435 * 2 - Add a "Access-Control-Allow-Origin:" header containing the same
436 body as the "Origin:" header from the request to the response
437 accepting the WebSocket handshake. If there is no "Origin:" header
438 in the request no header will be added to the response.
442 Example 1.9. Set cors_mode parameter
444 modparam("websocket", "cors_mode", 2)
447 4.8. verbose_list (int)
449 Allows to enable/disable the printing of debug messages when getting
450 the list of websocket connections. If enabled, it prints debug messages
451 every second for ping operations.
453 Default value is 0 (disabled).
455 Example 1.10. Set verbose_list parameter
457 modparam("websocket", "verbose_list", 1)
462 5.1. ws_handle_handshake()
463 5.2. ws_close([status, reason[, connection_id]])
465 5.1. ws_handle_handshake()
467 This function checks an HTTP GET request for the required headers and
468 values, and (if successful) upgrades the connection from HTTP to
471 This function can be used from ANY_ROUTE (but will only work in
472 event_route[xhttp:request]).
476 This function returns 0, stopping all further processing of the
477 request, when there is a problem.
479 Example 1.11. ws_handle_handshake usage
481 ws_handle_handshake();
484 5.2. ws_close([status, reason[, connection_id]])
486 This function closes a WebSocket connection.
488 The function returns -1 if there is an error and 1 if it succeeds.
490 The meaning of the parameters is as follows:
491 * status - an integer indicating the reason for closure.
492 * reason - a string describing the reason for closure.
493 * connection_id - the connection to close. If not specified the
494 connection the current message arrived on will be closed.
498 status and reason values SHOULD correspond to the definitions in
499 section 7.4 of RFC 6455. If these parameters are not used the defaults
500 of "1000" and "Normal closure" will be used.
502 This function can be used from ANY_ROUTE.
504 Example 1.12. ws_close usage
506 ws_close(4000, "Because I say so");
520 Provides the details of the first 50 WebSocket connections.
525 * order (optional) - “id_hash”, “used_desc”, or “used_asc”.
529 If no parameter is provided id_hash order is used.
533 kamcmd ws.dump used_asc
538 Starts the close handshake for the specified WebSocket connection.
543 * id - WebSocket connection ID.
552 Sends a Ping frame on the specified WebSocket connection.
557 * id - WebSocket connection ID.
566 Sends a Pong frame on the specified WebSocket connection.
571 * id - WebSocket connection ID.
580 Disables WebSockets preventing new connections from being established.
593 Enables WebSockets allowing new connections to be established.
597 WebSockets are enabled at start-up.
610 7.1. websocket:closed
612 7.1. websocket:closed
614 When defined, the module calls event_route[websocket:closed] when a
615 connection closes. The connection may be identified using the the $si
616 and $sp pseudo-variables.
618 Example 1.13. event_route[websocket:closed] usage
620 event_route[websocket:closed] {
621 xlog("L_INFO", "WebSocket connection from $si:$sp has closed\n");
625 8. Exported Pseudo Variables
631 Connection id of closed websocket connection. Can only be used in
632 websocket:closed event route.
634 Example 1.14. $ws_conid usage
636 event_route[websocket:closed] {
637 xlog("L_INFO", "WebSocket connection with id $ws_conid has closed\n");