🐇 Easy to use socket lib for Golang

Hare Sockets 🐇

Go codecov Mentioned in Awesome Go Go Report Card Codacy Badge Maintainability GoDoc Join the chat at https://gitter.im/hare-sockets/community Release License: MIT

Hare is a user-friendly lib for sockets in Golang. You can send and listen to TCP connections with a few lines of code.

Contents

Installation

First you need Go installed (version 1.12+ is required), then you can install Hare:

$ go get -u "github.com/leozz37/hare"

Import it in your code:

import "github.com/leozz37/hare"

(Optional) install jaguar for testing the sockets:

$ brew tap leozz37/jaguar

$ brew install jaguar

Quick start

Sample code for sending payloads:

package main

import (
    "github.com/leozz37/hare"
)

func main() {
    hare.Send(3000, "Hello, World")
}

Sample code for listening a port:

package main

import (
    "fmt"

    "github.com/leozz37/hare"
)

func main() {
    r, _ := hare.Listen("3000")

    for {
        if r.HasNewMessages() {
            fmt.Println(r.GetMessage())
        }
    }
}

Documentation

The library consists on two features: listen and send to a given port. You can check the full documentation on Godoc.

Send

Receives a port and a message, both as string and returns an error (if something goes wrong).

func Send(port, message string) error;

Usage example:

func main() {
    err := hare.Send(3000, "Hello, World")
    if err != nil {
        panic(err)
    }
}

Listen

Receives a port as string and returns a Listener struct and an error (if something goes wrong).

func Listen(port string) (*Listener, error);

Usage example:

func main() {
    r, _ := hare.Listen("3000")
    l, _ := hare.listen("3001")

    for {
        if r.HasNewMessages() {
            fmt.Println(r.GetMessage())
        } else if l.HasNewMessages() {
            fmt.Println(l.GetMessage())
        }
    }

Listener

The Listener struct returned by Listen() function has the following fields:

type Listener struct {
    SocketListener net.Listener
    HasNewMessages func() bool
    GetMessage     func() string
    Stop           func()
}

SocketListener is the socket connection.

listener.SocketListener, _ = net.Listen("tcp", "localhost:" + port)

HasNewMessages() function returns a bool being true with there's a new message:

func main() {
    r, _ := hare.Listen("3000")

    if r.HasNewMessages() {
        fmt.Println("There's a new message!")
    }
}

GetMessage() function returns a string with the last message received on the socket:

func main() {
    r, _ := hare.Listen("3000")

    if r.HasNewMessages() {
        fmt.Println(r.GetMessage())
    }
}

Stop() function closes the listener connection:

func main() {
    r, _ := hare.Listen("3000")
    hare.Send("3000", "Hey beauty")

    r.Stop()

    err := Send("3000", "This should fails")
    if err != nil {
        panic(err)
    }
}

Examples

You can check the example for code usages, like send and listen samples.

Since Hare only listen and send messages, here's a complete example:

package main

import (
    "fmt"
    "time"

    "github.com/leozz37/hare"
)

func listenSockets(port string) {
    r, _ := hare.Listen(port)

    for {
        if r.HasNewMessages() {
            fmt.Println(r.GetMessage())
        }
    }
}

func main() {
    go listenSockets("3000")
    go listenSockets("3001")

    for {
        hare.Send("3000", "Hello port 3000")
        hare.Send("3001", "Hello port 3001")
        time.Sleep(time.Second)
    }
}

Testing

To run the test suite, you can run with:

$ go test

If you want a more detailed report with coverage and an coverage.out file, do the following:

$ go test -v -covermode=count -coverprofile=coverage.out

Contributing

A full guideline about contributing to Alacritty can be found in the CONTRIBUTING.md file.

Issues

Use the search tool before opening a new issue.

Please provide source code and commit sha if you found a bug.

Review existing issues and provide feedback or react to them.

Pull requests

Open your pull request against develop.

You should add/modify tests to cover your proposed code changes.

Tests coverage should never go down from 90%.

If your pull request contains a new feature, please document it on the README.

Branching

The main is a regular branch which always contains the latest stable codebase and must never be broken.

The develop is a regular branch which always contains the latest development codebase and eventually can be broken. But you'll need to accept the sombrero of shame if you do that.

The release is a regular branch which contains a specific release version. You must use the following name convention: release-X.Y.Z, where X, Y and Z are: major, minor and patch version numbers.

The experimental is a temporary branch which contains a new feature or ideia. You must use the following name convention: experimental-brief-description.

The feature is a temporary branch which contains a new feature under development that latter will be merged against the development branch. You must use the following name convention: feature-brief-description.

The bugfix is a temporary branch which contains necessary fix to be applied after a specific release to be merged against the development branch. You must use the following name convention: bugfix-brief-description.

The hotfix is a temporary branch which contains a critical fix to be applied immediately and merged against the main and the development branches. You must use the following name convention: hotfix-brief-description.

Feel free to apply the labels from GitHub to the branches, they are very helpful.

Versioning

The project uses the semantic versioning 2.0.0 in order to control the version numbers.

Commiting

The main, develop and release branches have protection rules against push.

In order to contribute you must create a new branch following the branching guideline and once your work is done, open a pull request from your branch to the develop branch.

The pull request will trigger the test suites automatically and the code must pass all the tests and also be reviewed and approved before merged in the develop branch (or even main or release in case of a *fix).

Feel free to apply the labels from GitHub to the pull requests, they are very helpful.

License

Hare is released under the MIT License.

Owner
Leonardo Lima
Open Sourcerer 🧙‍♂️
Leonardo Lima
Similar Resources

Confluent's Apache Kafka Golang client

Confluent's Golang Client for Apache KafkaTM confluent-kafka-go is Confluent's Golang client for Apache Kafka and the Confluent Platform. Features: Hi

Dec 30, 2022

golang client library to Viessmann Vitotrol web service

Package go-vitotrol provides access to the Viessmann™ Vitotrol™ cloud API for controlling/monitoring boilers. See https://www.viessmann.com/app_vitoda

Nov 16, 2022

Golang push server cluster

Golang push server cluster

gopush-cluster gopush-cluster is a go push server cluster. Features light weight high performance pure golang implementation message expired offline m

Dec 28, 2022

A push notification server written in Go (Golang).

A push notification server written in Go (Golang).

gorush A push notification micro server using Gin framework written in Go (Golang) and see the demo app. Contents gorush Contents Support Platform Fea

Jan 8, 2023

websocket based messaging server written in golang

Guble Messaging Server Guble is a simple user-facing messaging and data replication server written in Go. Overview Guble is in an early state (release

Oct 19, 2022

mangos is a pure Golang implementation of nanomsg's "Scalablilty Protocols"

mangos is a pure Golang implementation of nanomsg's

mangos Mangos™ is an implementation in pure Go of the SP (“Scalability Protocols”) messaging system. These are colloquially known as a “nanomsg”. ❗ Th

Jan 1, 2023

Golang client for NATS, the cloud native messaging system.

NATS - Go Client A Go client for the NATS messaging system. Installation # Go client go get github.com/nats-io/nats.go/ # Server go get github.com/na

Jan 5, 2023

goczmq is a golang wrapper for CZMQ.

goczmq Introduction A golang interface to the CZMQ v4.2 API. Install Dependencies libsodium libzmq czmq For CZMQ master go get github.com/zeromq/goczm

Jan 5, 2023

Go (golang) bindings for the 0mq (zmq, zeromq) C API

NOTE: These gozmq bindings are in maintenance mode. Only critical bugs will be fixed. Henceforth I would suggest using @pebbe's actively maintained bi

Dec 9, 2022
Comments
  • Add a Gitter chat badge to README.md

    Add a Gitter chat badge to README.md

    leozz37/hare now has a Chat Room on Gitter

    @leozz37 has just created a chat room. You can visit it here: https://gitter.im/hare-sockets/community.

    This pull-request adds this badge to your README.md:

    Gitter

    If my aim is a little off, please let me know.

    Happy chatting.

    PS: Click here if you would prefer not to receive automatic pull-requests from Gitter in future.

socket.io library for golang, a realtime application framework.

go-socket.io go-socket.io is library an implementation of Socket.IO in Golang, which is a realtime application framework. Current this library support

Jan 8, 2023
An easy-to-use CLI client for RabbitMQ.

buneary, pronounced bun-ear-y, is an easy-to-use RabbitMQ command line client for managing exchanges, managing queues and publishing messages to exchanges.

Sep 3, 2022
Easy to use distributed event bus similar to Kafka
Easy to use distributed event bus similar to Kafka

chukcha Easy to use distributed event bus similar to Kafka. The event bus is designed to be used as a persistent intermediate storage buffer for any k

Dec 30, 2022
golang long polling library. Makes web pub-sub easy via HTTP long-poll server :smiley: :coffee: :computer:
golang long polling library.  Makes web pub-sub easy via HTTP long-poll server :smiley: :coffee: :computer:

golongpoll Golang long polling library. Makes web pub-sub easy via an HTTP long-poll server. New in v1.1 Deprecated CreateManager and CreateCustomMana

Jan 6, 2023
pubsub controller using kafka and base on sarama. Easy controll flow for actions streamming, event driven.

Psub helper for create system using kafka to streaming and events driven base. Install go get github.com/teng231/psub have 3 env variables for config

Sep 26, 2022
May 11, 2023
RPC realization with use RabbitMQ

RPC examples for RabbitMQ Description RPC example using RabbitMQ. In this example use the 6 tutorial RabbitMQ with some changes: exchange between clie

Nov 24, 2021
Github-notifications - Small script to alert me when I have notifications on Github. I use it in my Polybar conf

Github notification polybar widget This tool is meant to be used with Polybar, in order to let the user know when they have notifications on Github. R

Jan 26, 2022
go broker interface,you can use kafka,redis,pulsar etc.

broker go broker interface,you can use kafka,redis,pulsar etc. pulsar in docker run pulsar in docker docker run -dit \ --name pulsar-sever \ -p 6650:

Sep 8, 2022
graylog-golang is a full implementation for sending messages in GELF (Graylog Extended Log Format) from Go (Golang) to Graylog

graylog-golang is a full implementation for sending messages in GELF (Graylog Extended Log Format) from Go (Golang) to Graylog

Dec 5, 2022