Consulting, training, products, professional services
contact@wearefractal.comOpen source is at github.com/wearefractal
Subscribers/Publishers can be components of the same system or pieces of entirely different systems
PubSub is fundamentally an async control flow pattern
PubSub is a core piece of many people's "secret scale sauce"
Components need no knowledge of other components, just of the main bus
PubSub is used heavily in Javascript
Publishing is usually fire and forget. Publishers almost never care who is listening.
addEventListener(event, fn) for listening to DOM events
var el = document.getElementById("usernameField");
el.addEventListener("submit", checkValue, false);
ee.on(event, fn) for listening to events
ee.emit(event, args...) for emitting to events
var user = new User({name: "Mark"});
user.on('saved', function(){
console.log("User", this.name, "saved to the db!");
});
user.emit('saved');
EventEmitters are fire and forget. You don't know or care who is listening.
Connect to a channel and listen for events
var redis = require("redis");
var client = redis.createClient();
// set up message handler
client.on("message", function(channel, data){
console.log("Received", data, "from channel", channel);
});
client.subscribe("a nice channel");
Connect to a channel and publish for events
var redis = require("redis");
var client = redis.createClient();
client.publish("a nice channel", "some stuff happened!");
Redis channels are fire and forget. You don't know or care who is listening.
Open a bidirectional channel and send events back and forth
var WebSocket = require('ws');
var ws = new WebSocket('ws://example.com/test');
ws.on('message', function (data) {
console.log("Received", data);
});
ws.send('test');
git clone https://github.com/wearefractal/eventing-pubsub-workshop
cd eventing-pubsub-workshop
npm install
var redis = require("redis");
var client = redis.createClient();
// publish a Hello World! to test-channel
// Use client.publish
var redis = require("redis");
var client = redis.createClient();
// publish a Hello World! to test-channel
// Use client.publish
client.publish("test-channel", "Hello World!");
var redis = require("redis");
var client = redis.createClient();
var subclient = redis.createClient();
// a JSON object is being published to test-channel
var sendMessage = function(){
var obj = {
test: "Hello World!"
};
client.publish("test-channel", JSON.stringify(obj));
};
setTimeout(sendMessage, 2000);
// subscribe to the channel using subclient.subscribe
// since all redis messages need to be strings
// you will need to parse the object
// log the parsed message
var redis = require("redis");
var client = redis.createClient();
var subclient = redis.createClient();
// a JSON object is being published to test-channel
var sendMessage = function(){
var obj = {
test: "Hello World!"
};
client.publish("test-channel", JSON.stringify(obj));
};
setTimeout(sendMessage, 2000);
// subscribe to the channel using subclient.subscribe
// since all redis messages need to be strings
// you will need to parse the object
// log the parsed message
subclient.on("message", function(channel, msg) {
var parsed = JSON.parse(msg);
console.log(parsed);
});
subclient.subscribe("test-channel");
var WebSocket = require('ws');
var server = require('./setup'); // load server
var port = process.env.PORT || 9080;
var socket = new WebSocket('ws://localhost:'+port+'/test');
// in this scenario we are the browser
// we have an open websocket connection to a socket server
// on 'open', send it 'Hello!' and log what it sends back
var WebSocket = require('ws');
var server = require('./setup'); // load server
var port = process.env.PORT || 9080;
var socket = new WebSocket('ws://localhost:'+port+'/test');
// in this scenario we are the browser
// we have an open websocket connection to a socket server
// on 'open', send it 'Hello!' and log what it sends back
socket.on('open', function(){
socket.send('Hello!');
});
socket.on("message", function(data){
console.log(data);
});
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({port: process.env.PORT || 9080});
var client = require('./setup');
wss.on('connection', function(socket) {
// in this scenario we are the server
// when a socket connects set up a listener for the
// message event on the socket that
// logs the message and replies "Whats up!"
});
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({port: process.env.PORT || 9080});
var client = require('./setup');
wss.on('connection', function(socket) {
socket.on("message", function(msg){
console.log(msg);
socket.send("Whats up!");
});
});