Skip to content

Commit

Permalink
socket push notification
Browse files Browse the repository at this point in the history
Signed-off-by: maulana saputra <[email protected]>
  • Loading branch information
maulanaiij committed Nov 16, 2022
1 parent 3d1d6dd commit 37c4946
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ production

open browser menggunakan https dengan domain https://mern-excercise.local

message broker http://localhost:15672
`$ user : user`
`$ pass : password`

websocket http://localhost:8080

### reference
- [MERN Stack](https://github.com/bradtraversy/devconnector_2.0)
- [local ssl](https://hackerrdave.com/https-local-docker-nginx/)
Expand Down
7 changes: 5 additions & 2 deletions backend/config/messageBroker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ const connect = async () => {
}
};

const consume = async () => {
const consume = async (socket) => {
try {
connect().then(channel => {
channel.assertQueue('notification', { durable: false });
channel.consume('notification', msg => console.log('- Received', msg.content.toString()), { noAck: true });
channel.consume('notification', msg => {
console.log('- Received', msg.content.toString());
socket.emit('notification', msg.content.toString());
}, { noAck: true });
});
console.log("RabbitMQ Consume...");
} catch (err) {
Expand Down
4 changes: 3 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
"bcryptjs": "^2.4.3",
"config": "^3.3.7",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"express": "^4.18.1",
"express-validator": "^6.14.2",
"gravatar": "^1.8.2",
"jsonwebtoken": "^8.5.1",
"mongoose": "^6.5.3",
"normalize-url": "^5.3.0"
"normalize-url": "^5.3.0",
"socket.io": "^4.5.3"
},
"devDependencies": {
"concurrently": "^7.4.0",
Expand Down
9 changes: 8 additions & 1 deletion backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ const app = express();
db.connect();
db.open();

const cors = require('cors');
app.use(cors());

const server = app.listen(8080, () => console.log("WebSocket Connected..."));
const io = require('socket.io')(server, {cors: {origin: "*"}});
const socket = io.of('/');

messageBroker.connect();
messageBroker.consume();
messageBroker.consume(socket);

app.use(cookieParser());
app.use(express.json());
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ services:
volumes:
- ./backend/:/app
- /app/node_modules
ports:
- 8080:8080
restart: unless-stopped
depends_on:
- mongo
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ services:
volumes:
- ./backend/:/app
- /app/node_modules
ports:
- 8080:8080
restart: unless-stopped
depends_on:
- mongo
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"redux": "^4.2.0",
"redux-devtools-extension": "^2.13.9",
"redux-thunk": "^2.4.1",
"socket.io-client": "^4.5.3",
"uuid": "^9.0.0",
"web-vitals": "^2.1.4"
},
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useEffect } from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import io from 'socket.io-client';
import Navbar from './components/layout/Navbar';
import Landing from './components/layout/Landing';
import Register from './components/auth/Register';
Expand All @@ -15,6 +16,7 @@ import Posts from './components/posts/Posts';
import Post from './components/post/Post';
import NotFound from './components/layout/NotFound';
import PrivateRoute from './components/routing/PrivateRoute';
import { setAlert } from './actions/alert';

// Redux
import { Provider } from 'react-redux';
Expand All @@ -24,9 +26,14 @@ import { loadUser } from './actions/auth';
import './App.css';

const App = () => {
const socket = io("http://localhost:8080");
useEffect(() => {
// will get a 401 response from our API
store.dispatch(loadUser());
socket.on("notification", (response) => {
const result = JSON.parse(response);
store.dispatch(setAlert(result.message, "success"));
});
}, []);

return (
Expand Down

0 comments on commit 37c4946

Please sign in to comment.