Skip to content

Commit

Permalink
feat(sockets): enable sockets by default (#364)
Browse files Browse the repository at this point in the history
* feat(sockets): enable sockets by default

* typing

* migration
  • Loading branch information
samuelmasse authored Feb 16, 2022
1 parent fcf5d8c commit da24263
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/inject/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ For example with a server started on port 8080, and a messaging server started o
</html>
```

`hostUrl` should be the same base url that you get the inject.js script from. `messagingUrl` is simply the the url of the messaging server. To test this locally you will need to have a messaging server running as standalone from botpress (using the MESSAGING_ENDPOINT variable) with sockets enabled (ENABLE_EXPERIMENTAL_SOCKETS=true on the messaging server)
`hostUrl` should be the same base url that you get the inject.js script from. `messagingUrl` is simply the the url of the messaging server. To test this locally you will need to have a messaging server running as standalone from botpress (using the MESSAGING_ENDPOINT variable)
1 change: 1 addition & 0 deletions packages/server/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type MessagingEnv = EngineEnv & {
APM_ENABLED?: string
TWILIO_TESTING?: string
BILLING_ENDPOINT?: string
DISABLE_SOCKETS?: string
}

declare global {
Expand Down
33 changes: 33 additions & 0 deletions packages/server/src/migrations/1.1.0-user-tokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { getTableId, Migration } from '@botpress/messaging-engine'

export class UserTokensMigration extends Migration {
meta = {
name: UserTokensMigration.name,
description: 'Adds msg_user_tokens table',
version: '1.1.0'
}

async valid() {
return true
}

async applied() {
return this.trx.schema.hasTable(getTableId('msg_user_tokens'))
}

async up() {
// since it was possible to have this table created in the past with an experimental option, we delete it here
await this.trx.schema.dropTableIfExists(getTableId('msg_user_tokens'))

await this.trx.schema.createTable(getTableId('msg_user_tokens'), (table) => {
table.uuid('id').primary()
table.uuid('userId').references('id').inTable(getTableId('msg_users'))
table.string('token').notNullable()
table.timestamp('expiry').nullable()
})
}

async down() {
await this.trx.schema.dropTable(getTableId('msg_user_tokens'))
}
}
4 changes: 3 additions & 1 deletion packages/server/src/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { StatusMigration } from './0.1.19-status'
import { FixClientSchemaMigration } from './0.1.20-fix-client-schema'
import { ClientTokensMigration } from './0.1.21-client-tokens'
import { ChannelVersionsMigration } from './1.0.2-channel-versions'
import { UserTokensMigration } from './1.1.0-user-tokens'

export const Migrations: { new (): Migration }[] = [
InitMigration,
StatusMigration,
FixClientSchemaMigration,
ClientTokensMigration,
ChannelVersionsMigration
ChannelVersionsMigration,
UserTokensMigration
]
10 changes: 6 additions & 4 deletions packages/server/src/socket/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ export class SocketManager {
) {}

async setup(server: Server) {
if (yn(process.env.ENABLE_EXPERIMENTAL_SOCKETS)) {
this.ws = new Socket.Server(server, { serveClient: false, cors: { origin: '*' } })
this.ws.use(this.handleSocketAuthentication.bind(this))
this.ws.on('connection', this.handleSocketConnection.bind(this))
if (yn(process.env.DISABLE_SOCKETS)) {
return
}

this.ws = new Socket.Server(server, { serveClient: false, cors: { origin: '*' } })
this.ws.use(this.handleSocketAuthentication.bind(this))
this.ws.on('connection', this.handleSocketConnection.bind(this))
}

async destroy() {
Expand Down
5 changes: 0 additions & 5 deletions packages/server/src/user-tokens/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ export class UserTokenService extends Service {
}

async setup() {
if (!yn(process.env.ENABLE_EXPERIMENTAL_SOCKETS)) {
// let's not create a table we don't need for now
return
}

this.batcher = await this.batchingService.newBatcher(
'batcher_user_tokens',
[this.userService.batcher],
Expand Down
3 changes: 0 additions & 3 deletions packages/server/test/integration/client-tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ describe('ClientTokens', () => {
}

beforeAll(async () => {
// This should be reset after those tests
process.env.ENABLE_EXPERIMENTAL_SOCKETS = '1'

await setupApp()
clientTokens = app.clientTokens
querySpy = jest.spyOn(clientTokens as any, 'query')
Expand Down
3 changes: 0 additions & 3 deletions packages/server/test/integration/user-tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ describe('UserTokens', () => {
let state: { provider: Provider; client: Client; user: User; rawToken?: string; userToken?: UserToken }

beforeAll(async () => {
// This should be reset after those tests
process.env.ENABLE_EXPERIMENTAL_SOCKETS = '1'

await setupApp()
userTokens = app.userTokens
querySpy = jest.spyOn(userTokens as any, 'query')
Expand Down
1 change: 0 additions & 1 deletion test/jest.e2e.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { v4 as uuidv4 } from 'uuid'

const setup = async () => {
process.env.SKIP_LOAD_ENV = 'true'
process.env.ENABLE_EXPERIMENTAL_SOCKETS = 'true'
process.env.ADMIN_KEY = 'admin123'
process.env.DATABASE_URL = process.env.DATABASE_URL || path.join(__dirname, '.test-data', `${uuidv4()}.sqlite`)

Expand Down

0 comments on commit da24263

Please sign in to comment.