Skip to content

Commit cc204be

Browse files
committed
Many adjustments
1 parent cdc91c8 commit cc204be

File tree

3 files changed

+72
-59
lines changed

3 files changed

+72
-59
lines changed

README.md

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,60 +28,97 @@
2828

2929
## Installation
3030

31-
### Start the container
31+
### 1. Start the Refresh container
3232

33-
Bring up the refresh container (this is just
33+
Bring up the Refresh container (this is just
3434
[SSEHub](https://github.com/vgno/ssehub) with a little configuration):
3535

3636
```sh
3737
docker run --detach --name refresh --publish 8080:8080 ghcr.io/explodinglabs/refresh
3838
```
3939

40-
Port 8080 is exposed for you to `POST` to and for the browser connect to. This
41-
port is hard-coded in `refresh.js`. If you want to use a different port,
42-
download `refresh.js` and edit the port on the first line.
40+
Ensure the service is running with
4341

44-
### Create a Channel
42+
```sh
43+
docker logs refresh
44+
```
45+
46+
You should see:
47+
48+
```
49+
Listening on 0.0.0.0:8080
50+
Started client router thread.
51+
```
52+
53+
### 2. Create a Channel
54+
55+
Create a channel by simply posting an event to it:
4556

46-
Create the `changes` channel by simply post an event to it:
57+
```sh
58+
curl -X POST -d '{"id": 1, "event": "html", "data": null}' -w '%{response_code}' http://localhost:8080/refresh
59+
```
60+
61+
Here we used "refresh" as the channel name, but you could use your app's name.
62+
63+
#### Test the connection
64+
65+
To test, start a connection like:
4766

4867
```sh
49-
curl -X POST -d '{"id": 1, "event": "html", "data": null}' http://localhost:8080/changes
68+
curl http://localhost:8080/refresh
69+
```
70+
71+
You should see:
72+
5073
```
74+
:ok
5175
52-
This works because SSEHub is configured with `"allowUndefinedChannels": true`.
76+
```
5377

54-
### Add Refresh.js to Your Webpage
78+
### Add the Refresh script to your HTML
5579

56-
Include `refresh.js` in your html (put this at the bottom, right before
57-
`</body>`):
80+
Include the `refresh.js` script in your page (put this at the bottom, right
81+
before `</body>`):
5882

5983
```html
6084
<script
6185
type="text/javascript"
62-
data-events-uri=":8080/changes"
86+
data-events-uri=":8080/refresh"
6387
src="https://explodinglabs.github.io/refresh/refresh.js"
6488
async
6589
></script>
6690
```
6791

92+
`data-events-uri` is the location of the event source (the refresh container).
93+
If the protocol and host are omitted, `refresh.js` will use the ones in
94+
`window.location` (the current url in the browser window).
95+
96+
Another option is to set the events uri to a path like `/refresh` and then
97+
reverse proxy that, usually to `http://localhost:8080/refresh`.
98+
99+
Load your web page. The javascript console should show:
100+
101+
```
102+
eventSource open
103+
```
104+
68105
## Usage
69106

70-
Send a "html" or "js" event to refresh the entire page:
107+
To refresh the entire page, send a `html` or `js` event:
71108

72109
```sh
73-
curl -X POST -d '{"id": 1, "event": "html", "data": null}' http://localhost:8080/changes
110+
curl -X POST -d '{"id": 1, "event": "html", "data": null}' http://localhost:8080/refresh
74111
```
75112

76-
Send a "css" event to just update the styles:
113+
To update just the styles, send a `css` event:
77114

78115
```sh
79-
curl -X POST -d '{"id": 1, "event": "css", "data": null}' http://localhost:8080/changes
116+
curl -X POST -d '{"id": 1, "event": "css", "data": null}' http://localhost:8080/refresh
80117
```
81118

82119
### Vim Usage
83120

84-
Here's how I send a curl request when a file is saved in vim.
121+
Here's how I refresh the browser when a file is saved in vim.
85122

86123
Add to `~/.vimrc` (Vim 9+ only):
87124

@@ -90,9 +127,9 @@ def g:CbJobFailed(channel: channel, msg: string)
90127
echow msg
91128
enddef
92129
93-
autocmd BufWritePost *.html
130+
autocmd BufWritePost *.html,*.js
94131
call job_start(
95-
['curl', '--fail', '--silent', '--show-error', '-X', 'POST', '--data', '{"id": 1, "event": "html", "data": null}', 'http://localhost:8080/changes'],
132+
['curl', '--fail', '--silent', '--show-error', '-X', 'POST', '--data', '{"id": 1, "event": "html", "data": null}', 'http://localhost:8080/refresh'],
96133
{
97134
'exit_cb': function('g:CbRefreshBrowser'),
98135
'err_cb': function('g:CbJobFailed')
@@ -101,29 +138,10 @@ autocmd BufWritePost *.html
101138
102139
autocmd BufWritePost *.css
103140
call job_start(
104-
['curl', '--fail', '--silent', '--show-error', '-X', 'POST', '--data', '{"id": 1, "event": "css", "data": null}', 'http://localhost:8080/changes'],
141+
['curl', '--fail', '--silent', '--show-error', '-X', 'POST', '--data', '{"id": 1, "event": "css", "data": null}', 'http://localhost:8080/refresh'],
105142
{
106143
'exit_cb': function('g:CbRefreshBrowser'),
107144
'err_cb': function('g:CbJobFailed')
108145
}
109146
)
110147
```
111-
112-
## Troubleshooting
113-
114-
To debug connecting, start a connection from the command-line:
115-
116-
```sh
117-
curl http://localhost/changes
118-
```
119-
120-
### 404 Channel does not exist
121-
122-
An SSE channel needs to be created before connecting, otherwise you get
123-
"Channel does not exist". Channels are created when the first event is
124-
published to it. See [Create a Channel](#create_a_channel).
125-
126-
### 403 Forbidden
127-
128-
This can mean you've published to a domain that's not listed in
129-
`restrictPublish` in `ssehub.json`.

refresh.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
var changesEventSource = new EventSource("http://localhost:8080/changes");
1+
let uri = scriptTag?.dataset.uri || "";
2+
if (uri.startsWith(":")) {
3+
uri = `${window.location.protocol}//${window.location.hostname}${uri}`;
4+
}
5+
6+
const eventSource = new EventSource(uri);
27

38
function isExternal(url) {
49
var match = url.match(
@@ -36,22 +41,22 @@ function refreshCss() {
3641
}
3742
}
3843

39-
changesEventSource.onopen = (event) => {
40-
console.log("changesEventSource open");
44+
eventSource.onopen = (event) => {
45+
console.log("eventSource open");
4146
};
4247

43-
changesEventSource.onerror = (event) => {
44-
console.log("changesEventSource error");
48+
eventSource.onerror = (event) => {
49+
console.log("eventSource error");
4550
};
4651

47-
changesEventSource.addEventListener("html", (event) => {
52+
eventSource.addEventListener("html", (event) => {
4853
document.location.reload();
4954
});
5055

51-
changesEventSource.addEventListener("js", (event) => {
56+
eventSource.addEventListener("js", (event) => {
5257
document.location.reload();
5358
});
5459

55-
changesEventSource.addEventListener("css", (event) => {
60+
eventSource.addEventListener("css", (event) => {
5661
refreshCss();
5762
});

ssehub.json

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,12 @@
44
"port": 8080,
55
"bind-ip": "0.0.0.0",
66
"logdir": "./",
7-
"pingInterval": 5,
7+
"pingInterval": 15,
88
"pingEvent": false,
99
"threadsPerChannel": 2,
1010
"allowUndefinedChannels": true
1111
},
12-
"amqp": {
13-
"enabled": false,
14-
"host": "rabbitmq",
15-
"port": 5672,
16-
"user": "guest",
17-
"password": "guest",
18-
"exchange": "amq.fanout"
19-
},
2012
"default": {
21-
"cacheAdapter": "leveldb",
22-
"cacheLength": 500,
2313
"allowedOrigins": "*"
2414
}
2515
}

0 commit comments

Comments
 (0)