websocket

package module
v0.0.0-...-b880cd6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2021 License: MIT Imports: 16 Imported by: 0

README

lambda-go-websocket

Deploy websockets in AWS API Gateway with Go/Lambda. Store channel data in a DynamoDB table and deploy with Terraform.

Overview

The schema below show the architecture of the project:

overview

It contains the following key components:

  • a dynamodb database store the currently connected websockets as well as the associated channels. For now there is only one test channel
  • the Rx section on the top is made of a lambda and an API gateway that are maintaining websocket connections with users. The lamba tracks connections in dynamodb
  • the HTTP section on the bottom is mane of a lambda and an API gateway that can be used to discover connections to a channel and dispatch a message to all the users.

Before you start

There are a few issues, you need to be aware of:

  • all the name, including the api gateway, lambda and the dynamodb table are more than less hardcoded. It might be a good idea to rename them
  • the build is manual and must be performed before the deploy. A a result run make to start the deploy
  • you might want to change the region and your profile to connect to the right region/account
  • you should change the URL, Certificates, CORS to make the example fully operational

Deploy

Deploying the project should be pretty simple. Assuming you have Git, Go, Terraform and Make install, clone the project and run:

make

It should build the 2 lambda for Linux/x86_64. To deploy them and create all the resources, run:

cd terraform
terraform init
terraform apply

Test

There are several ways to test this code; an easy one is to rely on wscat

npm install -g wscat

The wscat command to run should as a terraform output. It would look like below:

wscat --connect wss://xxxxxxxx.execute-api.us-east-1.amazonaws.com/staging

The Websocket will echo your message. Once you've established the websocket, posting into the HTTP route would send a message to the websocket too:

curl https://yyyyyyyy.execute-api.us-east-1.amazonaws.com/staging/ \
  -XPOST -d'message' 

To continue

Do not hesitate to open an issue or add a PR to the repository. You can freely reuse this project, thank to the MIT LICENCE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequestResponse

func BadRequestResponse() events.APIGatewayProxyResponse

func HTTPBadRequestResponse

func HTTPBadRequestResponse() events.APIGatewayV2HTTPResponse

func HTTPInternalServerErrorResponse

func HTTPInternalServerErrorResponse() events.APIGatewayV2HTTPResponse

func HTTPOKResponse

func HTTPOKResponse() events.APIGatewayV2HTTPResponse

func InternalServerErrorResponse

func InternalServerErrorResponse() events.APIGatewayProxyResponse

func OkResponse

func OkResponse() events.APIGatewayProxyResponse

Types

type WebsocketLambda

type WebsocketLambda struct {
	ConnectionID      string `json:"ConnectionID"`
	ChannelID         string `json:"ChannelID"`
	RequestTimeEpoch  int64  `json:"RequestTimeEpoch"`
	ResourceID        string `json:"ResourceID"`
	EventType         string `json:"EventType"`
	ExtendedRequestID string `json:"ExtendedRequestID"`
	Endpoint          string `json:"Endpoint"`
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL