chat

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2023 License: MIT Imports: 9 Imported by: 0

README

Chat

When talking about end-to-end encryption a chat solution is something that usually comes into discussion. For this reason, this example illustrates how a chat application would work using goe2ee.

graph TD;
UserA<==>Registry
UserB<==>Registry
UserC<==>Registry
UserA--tcp-->UserB
UserC--tcp-->UserB
UserB--udp-->UserA
UserC--udp-->UserA
UserA--tcp-->UserC
UserB--tcp-->UserC

A central registry will be responsible for registering all users from the chat room, and notifying their peers that some users joined or left the chat. That means that each user will start a goe2ee and an HTTP server, where the HTTP server will be used to receive notifications from the registry. Users can connect using the goe2ee protocol via UDP or TCP (each user decides independently).

sequenceDiagram
activate user
user-->>user: start goe2ee server
user-->>user: start HTTP server
user->>registry: register me
activate registry
registry-->>registry: store new user data
loop every registeredUser
  registry--)registeredUser: notify user joined
  activate registeredUser
  registeredUser--)user: connect with goe2ee
  deactivate registeredUser
end
registry-->>user: ok
deactivate registry
loop every chat message
  registeredUser->>user: chat message
end
user->>registry: remove me
activate registry
loop every registeredUser
  registry--)registeredUser: notify user left
end
registry-->>user: ok
deactivate registry
deactivate user

The chat conversation will be automatically generated using the Excuser API, randomly generating messages between 1 and 10 seconds. All messages are sent to all registered users, so it behaves like a chat room/group. The client won't wait for a response from the server.

To execute this example you can first run the registry:

go run cmd/registry/main.go

That will print the registry address, then in other terminal tabs you can run as many users as you would like:

go run cmd/user/main.go -name <name> -registry <registry-address> -network <network>

For example, running a TCP user:

go run cmd/user/main.go -name Rafael -registry localhost:62465 -network tcp

and a UDP user:

go run cmd/user/main.go -name James -registry localhost:62465 -network udp

goe2ee-chat

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	System  bool   `json:"-"` // used for internal messages
	From    User   `json:"from"`
	Content string `json:"content"`
}

Message represents a chat message.

func GenerateMessage

func GenerateMessage(from User) (msg *Message, err error)

GenerateMessage creates a new message using the Excuser API [1].

[1] https://excuser-three.vercel.app/

func (Message) String

func (m Message) String() string

String returns a string representation of the message.

type Notification

type Notification struct {
	Event NotificationEvent `json:"event"`
	User  User              `json:"user"`
}

Notification represents a notification sent by the service registry.

func (Notification) Notify

func (n Notification) Notify(host string, port int64) (err error)

Notify sends a notification to a user.

type NotificationEvent

type NotificationEvent string

NotificationEvent represents the type of event that happened.

const (
	NotificationEventUserJoined NotificationEvent = "userJoined"
	NotificationEventUserLeft   NotificationEvent = "userLeft"
)

List of possible notification events.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry represents a service registry.

func NewRegistry

func NewRegistry(host string) *Registry

NewRegistry creates a new service registry.

func (Registry) Host

func (r Registry) Host() string

Host returns the host of the service registry.

func (Registry) Register

func (r Registry) Register(user User) (err error)

Register registers a new user in the service registry.

func (Registry) Unregister

func (r Registry) Unregister(userName string) (err error)

Unregister unregisters a user from the service registry.

type User

type User struct {
	Name       string `json:"name"`
	Network    string `json:"network"`
	Host       string `json:"host"`
	ServerPort int64  `json:"serverPort"` // depends on the network
	NotifyPort int64  `json:"notifyPort"` // always TCP
}

User represents a chat user.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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