Date post: | 17-Jul-2015 |
Category: |
Software |
Upload: | javier-arias-losada |
View: | 893 times |
Download: | 3 times |
full-stack-bus
communicating ALL your applications with RabbitMQ, postal.js and
javascript
javier arias losada @javier_arilos
imagine...
imagine your app needs growing (*)
it’s easy if you try
(*) by growing I mean functionality and/or performance and/or scalability
message bus
A message bus enables separate applications to work together, but in a decoupled fashion [1].
[1] http://bit.ly/1GI9W3B
message what?
Message bus technologies have been used in backend for more than 20 years: IBM MQSeries in 1992 [2].
[2] http://bit.ly/1GIbV8f
did you say backend?
Nowadays we can use message bus in the browser with the help of libraries such as postal.js [3].
[3] http://bit.ly/1zozbq9
seriously, what’s full-stack-bus?
By uniting frontend and backend bus technologies we can create applications that work out together in a decoupled fashion.
frontend => frontend
frontend => backend
backend => frontend
backend => backend
… but I already do that!!!!
frontend => frontend: eventEmitters, listeners, global variables...
frontend => backend: HTTP, Websockets...
backend => frontend: Websockets, Long-polling...
backend => backend : HTTP, sockets, shared Db...
Some of this are good mechanisms.
...others are just antipatterns…
Unifying approachs has its own beauty.
why then, a message bus?
Message bus allow to offer decoupled interfaces, based in Events/Messages.
No callbacks, no request-reply, producers and consumers are decoupled.
Off-the-shelf communication patterns: topics, queues, ...
is it for me?
Probably not. Message bus is not everybody’s cup of tea.
If your platform is growing a lot in terms of load and/or complexity then it will help.
can we now start with the tech?
This is one possible way to communicate all your applications.
postal.js
jsmodule
jsmodule
jsmodule
RabbitMQ
STOMP over
Websokets (SockJS)
μService μService μService
RabbitMQ
RabbitMQ is a Message Broker implementing of the AMQP protocol. [4]
Fast and easy to use.
Open source :-)
Almost certainly it is supported in your language /
platform
Very advanced concepts:
● Messaging: exchanges, queues, routing, …● Built-in clustering and replication
[4] http://www.rabbitmq.com
Postal.js
JS pub/sub library supporting some advanced subscription features [5]
➔ in-memory message bus - very loosely inspired by AMQP
➔ runs in the browser, or on the server using node.js➔ only pub/sub, no queues➔ opensource (> 1K in github)
[5] https://github.com/postaljs/postal.js
javascript
Javascript is THE full-stack language: runs on
the client, server, databases and even on a toaster [6]
[6] http://bit.ly/1uX9s44
In our sample game:
➔ node.js: μ-services
➔ angular: almost magical binding
.js
STOMP SockJS
[7] http://stomp.github.io/
[8] https://github.com/sockjs
[9] http://www.rabbitmq.com/web-stomp.html
STOMP Simple Text
Orientated Messaging
Protocol [7].
STOMP clients communicate
with any STOMP message
broker (eg. RabbitMQ).
SockJS is a browser JS library that provides a WebSocket-like object [8]
SockJS uses native WebSockets or a variety of browser-specific transport protocols.
RabbitMQ provides a plugin that implements STOMP over SockJS: Web-Stomp plugin [9].It is very simple to setup and use.
full-stack-bus ballgame example
application example: multiplayer ping-pong
[10] https://github.com/javierarilos/full-stack-bus-ballgame
postal.js
classificati
on
gamelog
ball mgmt
score aggregatorμService
rabbit->postal
rabbit->postal
logical structure & messages
ball management classification gamelogball managementball management
classificationclassification
score aggregator
gameloggamelog
topic/ballgamequeue/balls
sendBallreceiveBall
single.score
single.*
aggregated.scores
single.score
aggregated.scores
rabbit->postal
N x Browser
RabbitMQ
Node.js
gameloggameloggame management
single.new-player
single.pong single.ping single.ping-fail single.ball-picked
understood, show me the code!!!!
➔ RabbitMQ connection and subscriptions
http://bit.ly/1Gy7JcR#L140
➔ postal.js subscriptions
http://bit.ly/1Gy7JcR#L177
➔ routing msgs: RabbitMQ => postal.js
http://bit.ly/1Gy7JcR#L133
➔ sending a message to RabbitMQ
http://bit.ly/1Gy7JcR#L117
➔ aggregate scores μ-service
http://bit.ly/1Gy9V4f
media used. thanks to:
➔ Quentin Lebourgeois https://www.flickr.com/photos/quentinlebourgeois/5514792500/in/photostream/➔ pargon https://flic.kr/p/4J3Yqo➔ chriscroninimages www.flickr.com/photos/chriscroninimages/29963364➔ Jim Landon https://flic.kr/p/p3zyZv