Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add core model for Websocket Connections #342

Open
wants to merge 1 commit into
base: remove-package-lock-for-now
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
18 changes: 10 additions & 8 deletions packages/core/database/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const { mongoose} = require('./mongoose');
const { mongoose } = require('./mongoose');
const {
connectToDatabase,
disconnectFromDatabase,
createObjectId,
} = require('./mongo');
const {IndividualUser} = require('./models/IndividualUser');
const {OrganizationUser} = require('./models/OrganizationUser');
const {State} = require('./models/State');
const {Token} = require('./models/Token');
const {UserModel} = require('./models/UserModel');
const { IndividualUser } = require('./models/IndividualUser');
const { OrganizationUser } = require('./models/OrganizationUser');
const { State } = require('./models/State');
const { Token } = require('./models/Token');
const { UserModel } = require('./models/UserModel');
const { WebsocketConnection } = require('./models/WebsocketConnection');

module.exports = {
mongoose,
Expand All @@ -19,5 +20,6 @@ module.exports = {
OrganizationUser,
State,
Token,
UserModel
}
UserModel,
WebsocketConnection,
};
49 changes: 49 additions & 0 deletions packages/core/database/models/WebsocketConnection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { mongoose } = require('../mongoose');
const AWS = require('aws-sdk');

const schema = new mongoose.Schema({
connectionId: { type: mongoose.Schema.Types.String },
});

// Add a static method to get active connections
schema.statics.getActiveConnections = async function () {
try {
const connections = await this.find({}, 'connectionId');
return connections.map((conn) => ({
connectionId: conn.connectionId,
send: async (data) => {
const apigwManagementApi = new AWS.ApiGatewayManagementApi({
apiVersion: '2018-11-29',
endpoint: process.env.WEBSOCKET_API_ENDPOINT,
});

try {
await apigwManagementApi
.postToConnection({
ConnectionId: conn.connectionId,
Data: JSON.stringify(data),
})
.promise();
} catch (error) {
if (error.statusCode === 410) {
console.log(`Stale connection ${conn.connectionId}`);
await this.deleteOne({
connectionId: conn.connectionId,
});
} else {
throw error;
}
}
},
}));
} catch (error) {
console.error('Error getting active connections:', error);
throw error;
}
};

const WebsocketConnection =
mongoose.models.WebsocketConnection ||
mongoose.model('WebsocketConnection', schema);

module.exports = { WebsocketConnection };
Loading