KEMBAR78
SockJS Intro | ODP
SockJS Intro




               Ngoc Dao
Do you know WebSocket?
WebSocket
<script>
var sock = new WebSocket('ws://domain/my_prefix');

sock.onopen = function() {
  console.log('open');
};

sock.onmessage = function(e) {
  console.log('message', e.data);
};

sock.onclose = function() {
  console.log('close');               Server can push data
};                                  to browsers when it wants
</script>
WebSocket
    New browsers are required:
●   Internet Explorer: 10
●   Firefox: 6
●   Chrome: 4
●   Safari: 5
●   Opera: 12.10
SockJS
●   Provides WebSocket-like API
●   Supports all browsers, e.g. IE 6
●   Transports: websocket xhr-streaming xdr-
    streaming iframe-eventsource iframe-htmlfile
    xhr-polling xdr-polling iframe-xhr-polling jsonp-
    polling
●   Requires
     Client side: sockjs.js
     Server side: SockJS server side library
WebSocket vs SockJS
WebSocket                                        SockJS
<script>                                         <script>
var sock = new WebSocket('ws://domain/my_ws');   var sock = new SockJS('http://domain/my_prefix');
sock.onopen = function() {                       sock.onopen = function() {
 console.log('open');                             console.log('open');
};                                               };
sock.onmessage = function(e) {                   sock.onmessage = function(e) {
 console.log('message', e.data);                  console.log('message', e.data);
};                                               };
sock.onclose = function() {                      sock.onclose = function() {
 console.log('close');                            console.log('close');
};                                               };
</script>                                        </script>
Links
●   Client side:
    <script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
    https://github.com/sockjs/sockjs-client
●   Server side:
    Node.js: https://github.com/sockjs/sockjs-node
    Ruby: https://github.com/sockjs/sockjs-ruby
    Python: https://github.com/MrJoes/sockjs-tornado
    JVM:
     https://github.com/vert-x/vert.x
     https://github.com/ngocdaothanh/xitrum
    etc.
Links
●   Client side:
    <script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
    https://github.com/sockjs/sockjs-client
●   Server side:
    Node.js: https://github.com/sockjs/sockjs-node
    Ruby: https://github.com/sockjs/sockjs-ruby
    Python: https://github.com/MrJoes/sockjs-tornado
    JVM:
     https://github.com/vert-x/vert.x
     https://github.com/ngocdaothanh/xitrum
    etc.
Demo
https://github.com/sockjs/sockjs-node/tree/master/examples/echo
git clone git://github.com/sockjs/sockjs-node.git
cd sockjs-node/examples/echo
npm install
node server.js
SockJS protocol
If you want to implement a server side for SockJS,
or just want to know how SockJS works:
https://github.com/sockjs/sockjs-protocol
http://sockjs.github.com/sockjs-protocol/sockjs-protocol-0.3.3.html
SockJS vs Socket.IO
●   Socket.IO ≈ SockJS + α
●   SockJS is way simpler, closer to WebSocket
    => SockJS has lots of server side
    implementations for many languages

SockJS Intro

  • 1.
    SockJS Intro Ngoc Dao
  • 2.
    Do you knowWebSocket?
  • 3.
    WebSocket <script> var sock =new WebSocket('ws://domain/my_prefix'); sock.onopen = function() { console.log('open'); }; sock.onmessage = function(e) { console.log('message', e.data); }; sock.onclose = function() { console.log('close'); Server can push data }; to browsers when it wants </script>
  • 4.
    WebSocket New browsers are required: ● Internet Explorer: 10 ● Firefox: 6 ● Chrome: 4 ● Safari: 5 ● Opera: 12.10
  • 5.
    SockJS ● Provides WebSocket-like API ● Supports all browsers, e.g. IE 6 ● Transports: websocket xhr-streaming xdr- streaming iframe-eventsource iframe-htmlfile xhr-polling xdr-polling iframe-xhr-polling jsonp- polling ● Requires Client side: sockjs.js Server side: SockJS server side library
  • 6.
    WebSocket vs SockJS WebSocket SockJS <script> <script> var sock = new WebSocket('ws://domain/my_ws'); var sock = new SockJS('http://domain/my_prefix'); sock.onopen = function() { sock.onopen = function() { console.log('open'); console.log('open'); }; }; sock.onmessage = function(e) { sock.onmessage = function(e) { console.log('message', e.data); console.log('message', e.data); }; }; sock.onclose = function() { sock.onclose = function() { console.log('close'); console.log('close'); }; }; </script> </script>
  • 7.
    Links ● Client side: <script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script> https://github.com/sockjs/sockjs-client ● Server side: Node.js: https://github.com/sockjs/sockjs-node Ruby: https://github.com/sockjs/sockjs-ruby Python: https://github.com/MrJoes/sockjs-tornado JVM: https://github.com/vert-x/vert.x https://github.com/ngocdaothanh/xitrum etc.
  • 8.
    Links ● Client side: <script src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script> https://github.com/sockjs/sockjs-client ● Server side: Node.js: https://github.com/sockjs/sockjs-node Ruby: https://github.com/sockjs/sockjs-ruby Python: https://github.com/MrJoes/sockjs-tornado JVM: https://github.com/vert-x/vert.x https://github.com/ngocdaothanh/xitrum etc.
  • 9.
  • 10.
    SockJS protocol If youwant to implement a server side for SockJS, or just want to know how SockJS works: https://github.com/sockjs/sockjs-protocol http://sockjs.github.com/sockjs-protocol/sockjs-protocol-0.3.3.html
  • 11.
    SockJS vs Socket.IO ● Socket.IO ≈ SockJS + α ● SockJS is way simpler, closer to WebSocket => SockJS has lots of server side implementations for many languages