Skip to content
This repository was archived by the owner on Mar 1, 2021. It is now read-only.

Pattern/pub sub #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions factory/simple-factory/main.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
"use strict";
'use strict'

function Admin() {
console.log('Admin created!');
console.log('Admin created!')
}

function Customer() {
console.log('Customer created!');
console.log('Customer created!')
}

var UserFactory = {};
const UserFactory = {}
UserFactory.createUser = function(type) {
if (type == 'admin') {
return new Admin();
} else if (type == 'customer') {
return new Customer();
}
};
const allTypes = {
admin: Admin,
customer: Customer
}
return new allTypes[type]()
}

var customer = UserFactory.createUser('admin');
var customer = UserFactory.createUser('customer');
var customer = UserFactory.createUser('admin')
var customer = UserFactory.createUser('customer')
46 changes: 46 additions & 0 deletions pub-sub/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# PUB SUB PATTERN

![pub-sub](images/publish-subscribe.png)

## Pub/sub messaging has the following benefits:

- It decouples subsystems that still need to communicate. Subsystems can be managed independently, and messages can be properly managed even if one or more receivers are offline.

- It increases scalability and improves responsiveness of the sender. The sender can quickly send a single message to the input channel, then return to its core processing responsibilities. The messaging infrastructure is responsible for ensuring messages are delivered to interested subscribers.

- It improves reliability. Asynchronous messaging helps applications continue to run smoothly under increased loads and handle intermittent failures more effectively.

- It allows for deferred or scheduled processing. Subscribers can wait to pick up messages until off-peak hours, or messages can be routed or processed according to a specific schedule.

- It enables simpler integration between systems using different platforms, programming languages, or communication protocols, as well as between on-premises systems and applications running in the cloud.

- It facilitates asynchronous workflows across an enterprise.

- It improves testability. Channels can be monitored and messages can be inspected or logged as part of an overall integration test strategy.

- It provides separation of concerns for your applications. Each application can focus on its core capabilities, while the messaging infrastructure handles everything required to reliably route messages to multiple consumers.

## When to use this pattern

Use this pattern when:

- An application needs to broadcast information to a significant number of consumers.

- An application needs to communicate with one or more independently-developed applications or services, which may use different platforms, programming languages, and communication protocols.

- An application can send information to consumers without requiring real-time responses from the consumers.

- The systems being integrated are designed to support an eventual consistency model for their data.

- An application needs to communicate information to multiple consumers, which may have different availability requirements or uptime schedules than the sender.

This pattern might not be useful when:

- An application has only a few consumers who need significantly different information from the producing application.

- An application requires near real-time interaction with consumers.

### References

- [Microsoft](https://docs.microsoft.com/en-us/azure/architecture/patterns/publisher-subscriber)
- [Youtube - Dev Pleno - PT-BR](https://www.youtube.com/watch?v=B3YqxAFs-IQ)
44 changes: 44 additions & 0 deletions pub-sub/exemples/exemple-pub-sub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Socket {
constructor(channels = {}) {
this.channels = channels
}
subscribe(channelName, callback) {
const currentCbs = this.channels[channelName] || []
currentCbs.push(callback)
this.channels[channelName] = currentCbs
}
publish(channelName, message) {
if (this.channels[channelName]) {
this.channels[channelName].forEach(fn => fn(message))
}
}
unsubscribe(channelName, callback) {
const index = (this.channels[channelName] || []).indexOf(callback)
if (index >= 0) {
this.channels[channelName].splice(index, 1)
}
}
broadcast(message) {
Object.keys(this.channels).forEach(channel =>
this.publish(channel, message)
)
}
}

const socket = new Socket()

socket.subscribe('test1', message => {
console.log('test1', message)
})

socket.subscribe('basket', message => {
console.log('basket', message)
})

socket.publish('test1', {
message: 'just a test '
})

socket.broadcast({
message: 'TEST BROADCAST'
})
Binary file added pub-sub/images/publish-subscribe.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.