Skip to content

Commit

Permalink
subscription working
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen-Gordon committed Feb 21, 2024
1 parent 6ce1396 commit e6e6b1e
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 6 deletions.
131 changes: 128 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
"redux-persist": "^6.0.0",
"redux-thunk": "^3.1.0",
"reverse-mirage": "^1.0.3",
"socket.io": "^4.7.4",
"socket.io-client": "^4.7.4",
"tailwind-merge": "^2.1.0",
"truncate-eth-address": "^1.0.2",
"viem": "^2.5.0",
Expand Down
4 changes: 2 additions & 2 deletions sendPushNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const webPush = require('web-push');

const sendPushNotification = async () => {
const publicKey =
'BKygzgxdX4Y-kic62cOFL6SYlEjIXryAyd6i1lqvp2N9F8UQ9-eSCUpyoQxryU-Q-E1Xn0AxdFOZFvFxdVksKdI';
const privateKey = 'AWDM9R15IL7prFSo3EmNjIyoz3w8f1d5-bbp01emJHo';
'BK9FZUL3u5bgvs8NlurUeFesIq5dm3qEUwOlh3hL7wGPbNec2SELGLwjKU2jWv9P9GULDvlWlC04Lric-w8yEf8';
const privateKey = 'EctJtRAxWnq18ayGbnjcHQ';

webPush.setVapidDetails(
'mailto:[email protected]',
Expand Down
2 changes: 1 addition & 1 deletion src/app/notification/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const POST = async (req: NextRequest) => {
subscription: webPush.PushSubscription;
message: string;
};
console.log(message);
console.log(subscription);
try {
webPush.setVapidDetails(
`mailto:${process.env.NEXT_PUBLIC_WEB_PUSH_EMAIL}`,
Expand Down
28 changes: 28 additions & 0 deletions src/app/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const Server = require('socket.io');

// Create a socket.io server
const ioHandler = (req, res) => {
if (!res.socket.server.io) {
console.log('*First use, starting Socket.IO');
const io = new Server(res.socket.server);

// Listen for connection events
io.on('connection', (socket) => {
console.log(`Socket ${socket.id} connected.`);

// Listen for incoming messages and broadcast to all clients
socket.on('message', (message) => {
io.emit('message', message);
});

// Clean up the socket on disconnect
socket.on('disconnect', () => {
console.log(`Socket ${socket.id} disconnected.`);
});
});
res.socket.server.io = io;
}
res.end();
};

module.exports = ioHandler;

0 comments on commit e6e6b1e

Please sign in to comment.