Skip to content

Commit fce431b

Browse files
committed
first commit
0 parents  commit fce431b

13 files changed

+380
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# rabbitmq-messaging
2+
This is a quick Vue app to test out basic messaging features of RabbitMQ

package-lock.json

+154
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "rabbitmq-messaging",
3+
"version": "0.1.0",
4+
"description": "Quick Vue messaging app to test RabbitMQ basic features",
5+
"scripts": {
6+
"test": "echo \"Error: no test specified\" && exit 1"
7+
},
8+
"author": "Jérémy Castellano <jeremy.castellano@me.com>",
9+
"license": "ISC",
10+
"dependencies": {
11+
"amqplib": "^0.8.0",
12+
"vue": "^2.6.14",
13+
"vuex": "^3.6.2"
14+
},
15+
"devDependencies": {
16+
"@types/amqplib": "^0.8.2",
17+
"typescript": "^4.3.5"
18+
}
19+
}

src/amqp.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {config} from './app.config';
2+
import {IConfig} from "./global/interfaces/IConfig";
3+
import {Channel, connect} from "amqplib";
4+
import {Connection} from "amqplib";
5+
6+
export class Amqp {
7+
public config: IConfig;
8+
public currentChannel: Channel;
9+
10+
constructor() {
11+
this.config = config;
12+
}
13+
14+
public connect(): Promise<Connection> {
15+
return connect(this.config.url);
16+
}
17+
18+
public createChannel() {
19+
this.connect()
20+
.then(c => {
21+
console.log('Connection to ' + this.config.url + ':' + this.config.port + ' successful');
22+
return c.createChannel();
23+
})
24+
.then(ch => {
25+
console.log('Channel created');
26+
this.currentChannel = ch;
27+
})
28+
.catch(console.warn);
29+
};
30+
31+
public closeConnection(): void {
32+
this.currentChannel.close()
33+
.then(r => {
34+
console.log('Connection to channel closed : ' + r);
35+
});
36+
}
37+
}

src/app.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {IConfig} from "./global/interfaces/IConfig";
2+
3+
export const config: IConfig = {
4+
url: 'amqp://localhost',
5+
port: 5672,
6+
queue: 'messaging'
7+
}

src/components/App.vue

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<div>
3+
<Transcript />
4+
</div>
5+
</template>
6+
7+
<script lang="ts">
8+
import Vue from 'vue';
9+
import Transcript from "./Transcript.vue";
10+
11+
export default Vue.extend({
12+
name: 'App',
13+
components: {
14+
Transcript
15+
},
16+
created() {
17+
this.$publisher.sendMessage('Hi there !');
18+
}
19+
});
20+
</script>

src/components/Transcript.vue

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<template>
2+
<div>
3+
Transcript goes here
4+
</div>
5+
</template>
6+
7+
<script lang="ts">
8+
import Vue from 'vue';
9+
10+
export default Vue.extend({
11+
name: 'Transcript',
12+
data: () => ({
13+
messages: {}
14+
})
15+
});
16+
</script>

src/core.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Vue from "vue";
2+
import {Publisher} from "./publisher";
3+
import {Subscriber} from "./subscriber";
4+
import App from "./components/App.vue";
5+
6+
export class Core {
7+
public init(): void {
8+
new Vue({
9+
render: (h) => h(App)
10+
}).$mount('#app');
11+
12+
Core.initEnvironment();
13+
}
14+
15+
private static initEnvironment(): void {
16+
Vue.prototype.$publisher = new Publisher();
17+
Vue.prototype.$subscriber = new Subscriber();
18+
19+
Vue.prototype.$publisher.init();
20+
Vue.prototype.$subscriber.init();
21+
}
22+
}

src/global/interfaces/IConfig.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface IConfig {
2+
url: string,
3+
port: number,
4+
queue: string
5+
}

src/main.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {Core} from "./core";
2+
3+
const core: Core = new Core();
4+
5+
core.init();

src/publisher.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {Amqp} from "./amqp";
2+
3+
export class Publisher {
4+
private amqp: Amqp;
5+
6+
constructor() {
7+
this.amqp = new Amqp();
8+
}
9+
10+
private init(): void {
11+
this.amqp.createChannel();
12+
}
13+
14+
public sendMessage(message: string): void {
15+
this.amqp.currentChannel
16+
.assertQueue(this.amqp.config.queue)
17+
.then(r => {
18+
return this.amqp.currentChannel
19+
.sendToQueue(this.amqp.config.queue, Buffer.from(message));
20+
})
21+
}
22+
}

src/subscriber.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {Amqp} from "./amqp";
2+
3+
export class Subscriber {
4+
private amqp: Amqp;
5+
private _message: string;
6+
7+
constructor() {
8+
this.amqp = new Amqp();
9+
}
10+
11+
private init(): void {
12+
this.amqp.createChannel();
13+
}
14+
15+
public consumeMessage(): void {
16+
this.amqp.currentChannel
17+
.assertQueue(this.amqp.config.queue)
18+
.then(r => {
19+
this.amqp.currentChannel
20+
.consume(this.amqp.config.queue, msg => {
21+
if (msg !== null) {
22+
this._message = msg.content.toString();
23+
this.amqp.currentChannel.ack(msg);
24+
}
25+
})
26+
.then(r => {
27+
console.log(r);
28+
})
29+
})
30+
}
31+
32+
get message(): string {
33+
return this._message;
34+
}
35+
}

tsconfig.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"compilerOptions": {
3+
"noImplicitAny": false,
4+
"target": "ES2016",
5+
"module": "commonjs",
6+
"experimentalDecorators": true,
7+
"emitDecoratorMetadata": true,
8+
"sourceMap": true,
9+
"noEmitHelpers": false,
10+
"allowSyntheticDefaultImports": true
11+
},
12+
"rules": {
13+
"object-literal-sort-keys": false,
14+
"quotemark": true,
15+
"semicolor": true,
16+
"array-type": [true, "array"],
17+
"arrow-return-shorthand": [true, "multiline"],
18+
"switch-final-break": [true, "always"],
19+
"class-name": true,
20+
"arrow-parens": [true, "ban-single-arg-parens"],
21+
"indent": [true, "spaces", 4],
22+
"newline-before-return": true,
23+
"no-consecutive-blank-lines": true,
24+
"variable-name": [true, "check-format", "allow-leading-underscore", "allow-pascal-case"],
25+
"radix": false,
26+
"ordered-imports": [
27+
true,
28+
{
29+
"import-sources-order": "lowercase-last",
30+
"named-imports-order": "lowercase-first"
31+
}
32+
],
33+
"no-console": false
34+
},
35+
"include": ["src/**/*"]
36+
}

0 commit comments

Comments
 (0)