Skip to content

haruelrovix/gochat

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gochat

Simple Chat API using Go and WebSocket

Prerequisite

  1. Go
$ go version
go version go1.9.4 darwin/amd64
  1. SQLite
$ sqlite3 version
SQLite version 3.16.0 2016-11-04 19:09:39

How to Run

  1. Clone this repository
$ git clone https://github.com/haruelrovix/gochat.git && cd gochat
  1. Start SQLite using chat.db
$ sqlite3 chat.db
  1. Create below schema
CREATE TABLE chat(
  pk integer primary key autoincrement,
  message text,
  timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
);
  1. Check it again, just to make sure
sqlite> .schema
  1. Open another terminal, run
$ go run main.go
  1. If it asks to accept incoming network connections, allow it.

Accpet incoming network connections

  1. gochat listening on port 9000
[negroni] listening on :9000

Test the API

To test Post and Get, I used Insomnia REST Client.

  1. Post to http://localhost:9000/chat. It accepts one parameter: the string message to sent.

Post

  1. Get to http://localhost:9000/chat. It retrieves all previously-sent messages.

Get

WebSocket can be tested using Smart Websocket Client.

  1. Connect to ws://localhost:9000/ws, send a message.

WebSocket

  1. 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"
 }
]

Logging

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

Debugging

VS Code and Delve, a debugger for the Go programming language.

Debugging

About

Simple Chat API using Go and WebSocket

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages