Socket.io under the hood

13
Hao-kang Den @_hden Socket.io under the hood

description

Socket.io communication in detail

Transcript of Socket.io under the hood

Page 1: Socket.io under the hood

Hao-kang Den @_hden

Socket.io under the hood

Page 2: Socket.io under the hood
Page 3: Socket.io under the hood

Agenda

● Why socket.io?

● What’s socket.io? What’s websocket?

● Socket.io in detail

Page 4: Socket.io under the hood

HTTP

serverclient

(1) request

(2) response

WTF?

X

Page 5: Socket.io under the hood

Bi-directional Communication

serverclient

(1) signal

(2) acknowledgement

(3) signal

(4) acknowledgement

Page 6: Socket.io under the hood

Why don’t we use websocket?

● Standardized by○ RFC 6455

○ W3C

● IE9 says NO

Page 7: Socket.io under the hood

Why socket.io?+ native iOS & Android

Page 8: Socket.io under the hood

Socket.io Communication Layers

API

packet

communication (ws, jsonp, etc.)

Page 9: Socket.io under the hood
Page 10: Socket.io under the hood

API# client

socket.emit(‘foo’, param1, param2, ...)

socket.on ‘bar’, (param1, param2, ...) ->

console.log(‘yay got a bar!’)

# server

socket.on ‘foo’, (param1, param2, ...) ->

console.log(‘yay got a foo!’)

socket.emit(‘bar’, param1, param2, ...)

Page 11: Socket.io under the hood

Packet

socket.emit(‘foo’, param1, param2, ...)

# socket.io v0.9.x

packet = {

type: ‘event’

id: ‘unique packet id’

name: ‘foo’

args: [param1, param2, ...]

}

# socket.io v1.0.x

packet = {

type: ‘event’

id: ‘unique packet id’

name: ‘foo’

data: [param1, param2, ...]

}https://github.com/LearnBoost/socket.io-protocol

Page 12: Socket.io under the hood

Communication Layer# socket.io v0.9.x

packet = {

type: ‘event’

id: 1

name: ‘foo’

args: [param1, param2, ...]

}

# socket.io v0.9.x

5:1::{“name”:”foo”,”args”:[param1, param2, ...]}

https://github.com/LearnBoost/socket.io-spec

Page 13: Socket.io under the hood

Further Reading● http://davidwalsh.name/websocket● test/spec https://github.com/LearnBoost/socket.io/blob/master/test/socket.io.js● code https://github.com/LearnBoost/socket.io/blob/master/lib/socket.js

read!