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
3737docker 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
86123Add to ` ~/.vimrc ` (Vim 9+ only):
87124
@@ -90,9 +127,9 @@ def g:CbJobFailed(channel: channel, msg: string)
90127 echow msg
91128enddef
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
102139autocmd 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 ` .
0 commit comments