Skip to content

Instantly share code, notes, and snippets.

@vnnvanhuong
Created July 14, 2019 05:16
Show Gist options
  • Save vnnvanhuong/182dcf90ba05de227d1819abab1b014f to your computer and use it in GitHub Desktop.
Save vnnvanhuong/182dcf90ba05de227d1819abab1b014f to your computer and use it in GitHub Desktop.
// DDP Server
// Use an implementation of WebSocket Server such as SockJS
ddpServer.socket = WebSocketServer
ddpServer.socket.onData = (msg) => {
switch(msg):
case 'connect':
check whether WebSocket connection
socket.send({msg: 'connected', ...})
case 'method':
result = methodsMap.get(msg.methodID).invoke()
socket.send({'result': ...})
case 'sub':
publicationsMap.get(msg.subId).push(new Subscription)
socket.send({msg: 'ready', ...})
case ...
}
ddpServer.publish = (name) => {
publicationsMap.push(name)
}
dppServer.methods = (names) => {
methodsMap.push(names)
}
// DDP Client
// Simply use a WebSocket client to open connection
// and send messages to the WebSocket Server
ddpClient.ws = WebSocket
ddpClient.connect = () => {
ws.on('open', () => ws.send(EJSON({msg: 'connect', ...})))
}
ddpClient.call = () => {
ws.send(EJSON({msg: 'method', ...}))
}
ddpClient.subscribe = () => {
ws.send(EJSON({msg: 'sub', ...}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment