Simple Chat API using Go and WebSocket
$ go version
go version go1.9.4 darwin/amd64
$ sqlite3 version
SQLite version 3.16.0 2016-11-04 19:09:39
- Clone this repository
$ git clone https://github.com/haruelrovix/gochat.git && cd gochat
- Start SQLite using
chat.db
$ sqlite3 chat.db
- Create below schema
CREATE TABLE chat(
pk integer primary key autoincrement,
message text,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
- Check it again, just to make sure
sqlite> .schema
- Open another terminal, run
$ go run main.go
- If it asks to accept incoming network connections, allow it.
gochat
listening on port 9000
[negroni] listening on :9000
To test Post
and Get
, I used Insomnia REST Client.
Post
tohttp://localhost:9000/chat
. It accepts one parameter: the string message to sent.
Get
tohttp://localhost:9000/chat
. It retrieves all previously-sent messages.
WebSocket can be tested using Smart Websocket Client.
- Connect to
ws://localhost:9000/ws
, send a message.
Get
again, the message sent through WebSocket is also recorded.
[
{
"message": "Hi!",
"sent": "2018-02-18T18:38:12Z"
},
{
"message": "asl pls",
"sent": "2018-02-18T18:42:40Z"
},
{
"message": "hobby?",
"sent": "2018-02-18T18:42:53Z"
},
{
"message": "a lonely programmer",
"sent": "2018-02-18T18:48:33Z"
}
]
Negroni, Idiomatic HTTP Middleware for Golang.
[negroni] listening on :9000
[negroni] 2018-02-19T01:38:12+07:00 | 0 | 10.765667ms | localhost:9000 | POST /chat
[negroni] 2018-02-19T01:42:40+07:00 | 0 | 2.117104ms | localhost:9000 | POST /chat
[negroni] 2018-02-19T01:42:53+07:00 | 0 | 5.173429ms | localhost:9000 | POST /chat
[negroni] 2018-02-19T01:42:57+07:00 | 200 | 5.160381ms | localhost:9000 | GET /chat
[negroni] 2018-02-19T01:43:04+07:00 | 200 | 177.932µs | localhost:9000 | GET /chat
[negroni] 2018-02-19T01:50:52+07:00 | 200 | 1.622574ms | localhost:9000 | GET /chat
VS Code and Delve, a debugger for the Go programming language.