good morning!!!!

Skip to content
Snippets Groups Projects
README.md 1005 B
Newer Older
Anmol Sethi's avatar
Anmol Sethi committed
# Chat Example

Anmol Sethi's avatar
Anmol Sethi committed
This directory contains a full stack example of a simple chat webapp using nhooyr.io/websocket.
Anmol Sethi's avatar
Anmol Sethi committed

```bash
$ cd chat-example
$ go run . localhost:0
Anmol Sethi's avatar
Anmol Sethi committed
listening on http://127.0.0.1:51055
```

Visit the printed URL to submit and view broadcasted messages in a browser.

Anmol Sethi's avatar
Anmol Sethi committed
![Image of Example](https://i.imgur.com/VwJl9Bh.png)
Anmol Sethi's avatar
Anmol Sethi committed

## Structure

Anmol Sethi's avatar
Anmol Sethi committed
The frontend is contained in `index.html`, `index.js` and `index.css`. It sets up the
DOM with a scrollable div at the top that is populated with new messages as they are broadcast.
At the bottom it adds a form to submit messages.
The messages are received via the WebSocket `/subscribe` endpoint and published via
the HTTP POST `/publish` endpoint.
Anmol Sethi's avatar
Anmol Sethi committed

The server portion is `main.go` and `chat.go` and implements serving the static frontend
Anmol Sethi's avatar
Anmol Sethi committed
assets, the `/subscribe` WebSocket endpoint and the HTTP POST `/publish` endpoint.
Anmol Sethi's avatar
Anmol Sethi committed

The code is well commented. I would recommend starting in `main.go` and then `chat.go` followed by
`index.html` and then `index.js`.