You are on page 1of 11

SockJS Intro

Ngoc Dao

Do you know WebSocket?

WebSocket
<script>
var sock = new WebSocket('ws:!o"ain"y#pre$i%'&'
sock.onopen = $unction(& (
conso)e*)og('open'&'
+'
sock.onmessage = $unction(e& (
conso)e*)og('"essage', e*!ata&'
+'
sock.onclose = $unction(& (
conso)e*)og('c)ose'&'
+'
<script>
Server can pus- !ata
to browsers w-en it wants

WebSocket
New browsers are re.uire!:

Internet /%p)orer: 01

2ire$o%: 3

4-ro"e: 5

Sa$ari: 6

7pera: 08*01

SockJS

9rovi!es WebSocket:)ike ;9I

Supports a)) browsers, e*g* I/ 3

<ransports: websocket %-r:strea"ing %!r:


strea"ing i$ra"e:eventsource i$ra"e:-t")$i)e
%-r:po))ing %!r:po))ing i$ra"e:%-r:po))ing =sonp:
po))ing

>e.uires
4)ient si!e: sock=s*=s
Server si!e: SockJS server si!e )ibrary

WebSocket vs SockJS
WebSocket
<script>
var sock = new WebSocket('ws:!o"ain"y#ws'&'
sock.onopen = $unction(& (
conso)e*)og('open'&'
+'
sock.onmessage = $unction(e& (
conso)e*)og('"essage', e*!ata&'
+'
sock.onclose = $unction(& (
conso)e*)og('c)ose'&'
+'
<script>
SockJS
<script>
var sock = new SockJS('-ttp:!o"ain"y#pre$i%'&'
sock.onopen = $unction(& (
conso)e*)og('open'&'
+'
sock.onmessage = $unction(e& (
conso)e*)og('"essage', e*!ata&'
+'
sock.onclose = $unction(& (
conso)e*)og('c)ose'&'
+'
<script>

?inks

4)ient si!e:
<script src=@-ttp:c!n*sock=s*orgsock=s:1*A*"in*=s@><script>
-ttps:git-ub*co"sock=ssock=s:c)ient

Server si!e:
No!e*=s: -ttps:git-ub*co"sock=ssock=s:no!e
>uby: -ttps:git-ub*co"sock=ssock=s:ruby
9yt-on: -ttps:git-ub*co"BrJoessock=s:torna!o
JCB:
-ttps:git-ub*co"vert:%vert*%
-ttps:git-ub*co"ngoc!aot-an-%itru"
etc*

?inks

4)ient si!e:
<script src=@-ttp:c!n*sock=s*orgsock=s:1*A*"in*=s@><script>
-ttps:git-ub*co"sock=ssock=s:c)ient

Server si!e:
No!e*=s: -ttps:git-ub*co"sock=ssock=s:no!e
>uby: -ttps:git-ub*co"sock=ssock=s:ruby
9yt-on: -ttps:git-ub*co"BrJoessock=s:torna!o
JCB:
-ttps:git-ub*co"vert:%vert*%
-ttps:git-ub*co"ngoc!aot-an-%itru"
etc*

De"o
-ttps:git-ub*co"sock=ssock=s:no!etree"astere%a"p)esec-o
git c)one git:git-ub*co"sock=ssock=s:no!e*git
c! sock=s:no!ee%a"p)esec-o
np" insta))
no!e server*=s

SockJS protoco)
I$ you want to i"p)e"ent a server si!e $or SockJS,
or =ust want to know -ow SockJS works:
-ttps:git-ub*co"sock=ssock=s:protoco)
-ttp:sock=s*git-ub*co"sock=s:protoco)sock=s:protoco):1*A*A*-t")

SockJS vs Socket*I7

Socket*I7 D SockJS E F

SockJS is way si"p)er, c)oser to WebSocket


=> SockJS -as )ots o$ server si!e
i"p)e"entations $or "any )anguages

You might also like