-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[-] minor changes to the sample SSE implementation (refactored to use…
… serviceworker) [-] updated SSE README for clarity
- Loading branch information
1 parent
627108a
commit deab16e
Showing
5 changed files
with
103 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const sse = (url, config = {}) => { | ||
const { | ||
onMessage, | ||
onError, | ||
initialBackoff = 10, // milliseconds | ||
maxBackoff = 15 * 1000, // 15 seconds | ||
backoffStep = 50, // milliseconds | ||
} = config; | ||
|
||
let backoff = initialBackoff, | ||
sseRetryTimeout = null; | ||
|
||
const start = () => { | ||
const source = new EventSource(url); | ||
const configState = { initialBackoff, maxBackoff, backoffStep, backoff }; | ||
|
||
source.onopen = () => { | ||
clearTimeout(sseRetryTimeout); | ||
// reset backoff to initial, so further failures will again start with initial backoff | ||
// instead of previous duration | ||
backoff = initialBackoff; | ||
configState.backoff = backoff; | ||
}; | ||
|
||
source.onmessage = (event) => { | ||
onMessage && onMessage(event, configState); | ||
}; | ||
|
||
source.onerror = (err) => { | ||
source.close(); | ||
if (!backoffStep) { | ||
onError && onError(err, configState); | ||
return; | ||
} | ||
|
||
clearTimeout(sseRetryTimeout); | ||
// reattempt connecting with *linear* backoff | ||
sseRetryTimeout = self.setTimeout(() => { | ||
start(url, onMessage); | ||
if (backoff < maxBackoff) { | ||
backoff += backoffStep; | ||
if (backoff > maxBackoff) { | ||
backoff = maxBackoff; | ||
} | ||
} | ||
}, backoff); | ||
onError && onError(err, configState); | ||
}; | ||
}; | ||
return start; | ||
}; | ||
|
||
onmessage = (e) => { | ||
sse(e?.data?.url, { | ||
onMessage: (event) => { | ||
postMessage(event?.data); | ||
}, | ||
onError: (err, attrs) => { | ||
postMessage({ error: "SSE failed", ...attrs }); | ||
}, | ||
})(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/bnkamalesh/webgo/v6 | ||
|
||
go 1.17 | ||
go 1.18 |