Skip to content
Merged
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
11 changes: 10 additions & 1 deletion assets/js/phoenix/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ import Connection from "./connection"
* }
* ````
*
* @param {boolean} [opts.reconnectWhenHidden] - When true, the socket also reconnects while the
* page is hidden, for example in a background tab or on a sleeping phone.
*
* Defaults to false. The socket then waits until the page is visible again before it reconnects.
* This avoids reconnect attempts that stall on mobile devices and delay the connection after the
* device wakes up.
*
* @param {Function} [opts.rejoinAfterMs] - The optional function that returns the millisec
* rejoin interval for individual channels.
*
Expand Down Expand Up @@ -186,6 +193,7 @@ export default class Socket {
return [10, 50, 100, 150, 200, 250, 500, 1000, 2000][tries - 1] || 5000
}
}
this.reconnectWhenHidden = opts.reconnectWhenHidden || false
this.logger = opts.logger || null
if(!this.logger && opts.debug){
this.logger = (kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }
Expand All @@ -198,7 +206,8 @@ export default class Socket {
this.heartbeatTimer = null
this.pendingHeartbeatRef = null
this.reconnectTimer = new Timer(() => {
if(this.pageHidden){
// handleVisibilityChange reconnects once the page is visible again
if(this.pageHidden && !this.reconnectWhenHidden){
this.log("Not reconnecting as page is hidden!")
this.teardown()
return
Expand Down
50 changes: 50 additions & 0 deletions assets/test/socket_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe("with transports", function (){
expect(socket.logger).toBeNull()
expect(socket.binaryType).toBe("arraybuffer")
expect(typeof socket.reconnectAfterMs).toBe("function")
expect(socket.reconnectWhenHidden).toBe(false)
})

it("supports closure or literal params", function (){
Expand Down Expand Up @@ -350,6 +351,55 @@ describe("with transports", function (){
})
})

describe("when the connection drops while the page is hidden", function (){
const dropConnection = (opts = {}) => {
socket = new Socket("/socket", {transport: StubWebSocket, reconnectAfterMs: () => 10, ...opts})
socket.connect()
connections[0].open()
Object.defineProperty(document, "visibilityState", {value: "hidden", writable: true})
connections[0].finishClose(1006)
}

beforeEach(function (){
connections = []
jest.useFakeTimers()
Object.defineProperty(document, "visibilityState", {value: "visible", writable: true})
})

afterEach(function (){
jest.useRealTimers()
Object.defineProperty(document, "visibilityState", {value: "visible", writable: true})
})

it("does not reconnect by default", function (){
dropConnection()
jest.advanceTimersByTime(5000)

expect(connections.length).toBe(1)
expect(socket.conn).toBeNull()
})

it("reconnects with reconnectWhenHidden", function (){
dropConnection({reconnectWhenHidden: true})
jest.advanceTimersByTime(5000)

expect(connections.length).toBe(2)
expect(socket.conn).toBe(connections[1])
})

it("replaces a connection that is still connecting when the page is visible again with reconnectWhenHidden", function (){
dropConnection({reconnectWhenHidden: true})
jest.advanceTimersByTime(5000)

Object.defineProperty(document, "visibilityState", {value: "visible", writable: true})
socket.handleVisibilityChange()
jest.advanceTimersByTime(5000)

expect(connections.length).toBe(3)
expect(socket.conn).toBe(connections[2])
})
})

describe("resume", function (){
// Chrome does not reliably fire visibilitychange when a frozen page is
// resumed, see https://issues.chromium.org/issues/547062449.
Expand Down
Loading