Skip to content

Commit

Permalink
feat: treat any incoming message as hearbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 18, 2023
1 parent e7829fb commit 9c65613
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master

- Treat any incoming message as keepalive. ([@palkan][])

See the corresponding [Rails PR](https://github.com/rails/rails/pull/49168).

## 0.7.9 (2023-10-13)

- Set `cable.sessionId` when using Action Cable (base) protocol. ([@palkan][])
Expand Down
7 changes: 6 additions & 1 deletion packages/core/action_cable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ export class ActionCableProtocol {

let { type, identifier, message, reason, reconnect } = msg

if (type === 'ping') return this.cable.keepalive(msg.message)
if (type === 'ping') {
return this.cable.keepalive(msg.message)
} else {
// Any incoming message may be considered as a heartbeat
this.cable.keepalive()
}

if (type === 'welcome') {
let sessionId = msg.sid
Expand Down
23 changes: 13 additions & 10 deletions packages/core/action_cable_ext/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ export class ActionCableExtendedProtocol extends ActionCableProtocol {
return super.receive(msg)
}

if (type === 'confirm_history') {
this.logger.debug('history result received', msg)
return
}

if (type === 'reject_history') {
this.logger.warn('failed to retrieve history', msg)
return
}

if (type === 'ping') {
if (!this.restoreSince === false) {
this.restoreSince = now()
Expand All @@ -57,6 +47,19 @@ export class ActionCableExtendedProtocol extends ActionCableProtocol {
}

return this.cable.keepalive(msg.message)
} else {
// Any incoming message may be considered as a heartbeat
this.cable.keepalive()
}

if (type === 'confirm_history') {
this.logger.debug('history result received', msg)
return
}

if (type === 'reject_history') {
this.logger.warn('failed to retrieve history', msg)
return
}

if (type === 'welcome') {
Expand Down
9 changes: 6 additions & 3 deletions packages/core/create-cable/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CableTransport extends TestTransport {
this.subscriptions = {}
this.sendLater({ type: 'welcome' })
this.pingTid = setInterval(() => {
this.sendLater({ type: 'ping' })
this.sendLater({ type: 'ping', message: Date.now() })
}, 500)
return promise
}
Expand Down Expand Up @@ -150,7 +150,8 @@ describe('Action Cable protocol communication', () => {
reject(Error('Timed out to received pings'))
}, 1000)

cable.on('keepalive', () => {
cable.on('keepalive', msg => {
if (!msg) return
clearTimeout(tid)
resolve()
})
Expand All @@ -176,7 +177,9 @@ describe('Action Cable protocol communication', () => {
reject(Error('Timed out to received pings'))
}, 1000)

cable.on('keepalive', async () => {
cable.on('keepalive', async msg => {
if (!msg) return

clearTimeout(tid)

await waitSec(0)
Expand Down

0 comments on commit 9c65613

Please sign in to comment.