messaging

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2020 License: MIT Imports: 17 Imported by: 0

README

self messaging client GoDoc Go Report Card Build Status

A messaging client for go for use with self's messaging service

Installation

To start using this client, you can run:

$ go get github.com/selfid-net/self-messaging-client

Usage

To create a new messaging client

package main

import (
    messaging "github.com/selfid-net/self-messaging-client"
    msgproto "github.com/selfid-net/self-messaging-client/proto"
)

func main() {
    appID := "5d41402abc4b2a76b9719d911017c592" // the ID of your application
    device := "1"                               // the device identifier you are connecting as
    appKey := "secret-key"                      // the applications private key

    // create a new messaging client
    client, err := messaging.New("wss://messaging.selfid.net", appID, device, appKey)
}

Additional optional parameters can be specified to the client like:

func main() {
    ...

    client, err := messaging.New("wss://messaging.selfid.net", appID, device, appKey, messaging.AutoReconnect(true))
}

You can send a message by using the following:

func main() {
    ...

    msg := &msgproto.Message{
        Id: uuid.New().String(),                      // the id of the request
        Type: msgproto.MsgType_MSG,                   // the type of message
        Sender: "5d41402abc4b2a76b9719d911017c592:1", // constructed from app ID and device
        Recipient: "12345678910:aeH2o21",             // the recipients self ID and device
        Ciphertext: []byte("hello"),                  // the messages payload
    }

    err = client.Send(msg)
}

There are two ways to receive a message:

func main() {
    ...

    // read message
    for {
        msg, err := client.Receive()
    }

    // or read via channel
    mch := client.ReceiveChan()
    for {
        if !client.IsClosed() {
            return
        }

        msg := <-mch
    }
}

Versioning

For transparency into our release cycle and in striving to maintain backward compatibility, this project is maintained under the Semantic Versioning guidelines.

Code and documentation copyright since 2019 Self ID LTD.

Code released under the MIT License.

Documentation

Index

Constants

View Source
const (
	DefaultBufferSize = 128
	DefaultTimeout    = time.Second * 10
	DefaultDeadline   = time.Second * 10
	DefaultRetries    = 30
)

Variables

View Source
var (
	CloseMessage = websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")
	TimeFunc     = NewTime().Now
)
View Source
var NtpServer = "time.google.com"

Functions

func AutoReconnect

func AutoReconnect(enabled bool) func(c *Client) error

AutoReconnect enables retrying a connection if it closes unexpectedly

func ReadDeadline

func ReadDeadline(deadline time.Duration) func(c *Client) error

ReadDeadline sets the tcp read timeout

func ReceiveBuffer

func ReceiveBuffer(sz int) func(c *Client) error

ReceiveBuffer sets the size of the receive buffer

func SendBuffer

func SendBuffer(sz int) func(c *Client) error

SendBuffer sets the size of the send buffer

Types

type ACLRule

type ACLRule struct {
	Source  string    `json:"acl_source"`
	Expires time.Time `json:"acl_exp"`
}

type Client

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

Client connection for self messaging

func New

func New(endpoint, selfID, deviceID, privateKey string, opts ...func(*Client) error) (*Client, error)

New create a new messaging client

func (*Client) BlockSender

func (c *Client) BlockSender(selfID string) error

BlockSender blocks messages from a given sender

func (*Client) Close

func (c *Client) Close()

func (*Client) IsClosed

func (c *Client) IsClosed() bool

IsClosed returns true if the connection is closed

func (*Client) JWSRegister

func (c *Client) JWSRegister(id string)

JWSRegister registers a jws request by id

func (*Client) JWSRequest

func (c *Client) JWSRequest(id string, m *msgproto.Message) (chan *msgproto.Message, error)

JWSRequest makes a JWS request and returns the response

func (*Client) JWSResponse

func (c *Client) JWSResponse(id string, timeout time.Duration) (*msgproto.Message, error)

JWSResponse waits for a message response for a given JWS request

func (*Client) ListACLRules

func (c *Client) ListACLRules() ([]ACLRule, error)

ListACLRules returns all active ACL rules for the authenticated identity

func (*Client) PermitAll

func (c *Client) PermitAll() error

PermitAll permits messages from all identities

func (*Client) PermitSender

func (c *Client) PermitSender(selfID string, exp time.Time) error

PermitSender permits messages from a given sender

func (*Client) Receive

func (c *Client) Receive() (*msgproto.Message, error)

Receive receive a message

func (*Client) ReceiveChan

func (c *Client) ReceiveChan() chan *msgproto.Message

ReceiveChan returns a channel of all incoming messages

func (*Client) Send

func (c *Client) Send(m *msgproto.Message) error

Send send a message

type Time

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

Time contains information about the NTP server

func NewTime

func NewTime() *Time

func (*Time) Now

func (c *Time) Now() time.Time

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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