• Stars
    star
    679
  • Rank 63,892 (Top 2 %)
  • Language
    Elixir
  • Created almost 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Socket wrapping for Elixir.

Elixir sockets made decent

This library wraps gen_tcp, gen_udp and gen_sctp, ssl and implements websockets and socks.

Installation

In your mix.exs file

defp deps do
  [
    # ...
    {:socket, "~> 0.3"},
    # ...
  ]
end

Then run mix deps.get to install

Examples

defmodule HTTP do
  def get(uri) when is_binary(uri) or is_list(uri) do
    get(URI.parse(uri))
  end

  def get(%URI{host: host, port: port, path: path}) do
    sock = Socket.TCP.connect! host, port, packet: :line
    sock |> Socket.Stream.send! "GET #{path || "/"} HTTP/1.1\r\nHost: #{host}\r\n\r\n"

    [_, code, text] = Regex.run ~r"HTTP/1.1 (.*?) (.*?)\s*$", sock |> Socket.Stream.recv!

    headers = headers([], sock) |> Enum.into(%{})

    sock |> Socket.packet! :raw
    body = sock |> Socket.Stream.recv!(String.to_integer(headers["Content-Length"]))

    { { String.to_integer(code), text }, headers, body }
  end

  defp headers(acc, sock) do
    case sock |> Socket.Stream.recv! do
      "\r\n" ->
        acc

      line ->
        [_, name, value] = Regex.run ~r/^(.*?):\s*(.*?)\s*$/, line

        headers([{ name, value } | acc], sock)
    end
  end
end

Websockets

Client

socket = Socket.Web.connect! "echo.websocket.org"
socket |> Socket.Web.send! { :text, "test" }
socket |> Socket.Web.recv! # => {:text, "test"}

In order to connect to a TLS websocket, use the secure: true option:

socket = Socket.Web.connect! "echo.websocket.org", secure: true

The connect! function also accepts other parameters, most notably the path parameter, which is used when the websocket server endpoint exists on a path below the domain ie. "example.com/websocket":

socket = Socket.Web.connect! "example.com", path: "/websocket"

Note that websocket servers send ping messages. A pong reply from your client tells the server to keep the connection open and to send more data. If your client doesn't send a pong reply then the server will close the connection. Here's an example of how to get get both the data you want and reply to a server's pings:

socket = Socket.Web.connect! "echo.websocket.org"
case socket |> Socket.Web.recv! do
  {:text, data} ->
    # process data
  {:ping, _ } ->
    socket |> Socket.Web.send!({:pong, ""})
end

Server

server = Socket.Web.listen! 80
client = server |> Socket.Web.accept!

# here you can verify if you want to accept the request or not, call
# `Socket.Web.close!` if you don't want to accept it, or else call
# `Socket.Web.accept!`
client |> Socket.Web.accept!

# echo the first message
client |> Socket.Web.send!(client |> Socket.Web.recv!)

More Repositories

1

amnesia

Mnesia wrapper for Elixir.
Elixir
682
star
2

ruby-tesseract-ocr

A Ruby wrapper library to the tesseract-ocr API.
Ruby
627
star
3

ruby-thread

Various extensions to the base thread library.
Ruby
525
star
4

rust-ffmpeg

Safe FFmpeg wrapper.
Rust
443
star
5

rust-tun

TUN device creation and handling.
Rust
215
star
6

elixir-datastructures

Datastructures for Elixir.
Elixir
212
star
7

rust-ffmpeg-sys

moved to meh/rust-ffmpeg
Rust
147
star
8

cancer

It's terminal.
Rust
129
star
9

urna

REST in peace.
Elixir
95
star
10

smart-referer

MOVED to GitLab: https://gitlab.com/smart-referer/smart-referer/
JavaScript
95
star
11

reagent

You need more reagents to conjure this server.
Elixir
92
star
12

dux

DUX MEA LUX
Rust
89
star
13

exquisite

LINQ-like match_spec generation for Elixir.
Elixir
78
star
14

rust-packet

Network packet handling for Rust.
Rust
76
star
15

cauldron

I wonder what kind of Elixir is boiling in there.
Elixir
74
star
16

jazz

Yet another library to handle JSON in Elixir.
Elixir
61
star
17

rorschach

And I'll look down and whisper "GNO."
Rust
59
star
18

screenruster

X11 screen saver and locker.
Rust
53
star
19

miserve

Servo based minimal browser.
PostScript
48
star
20

blender

Blend in the crowd by faking to be the most common Firefox browser version, operating system and other stuff.
Ruby
42
star
21

rust-uinput

Linux uinput wrapper.
Rust
39
star
22

lissio

E vai col lissio!
Ruby
39
star
23

rust-terminfo

Terminal information for Rust.
Rust
38
star
24

cesso

CSV handling library for Elixir.
Elixir
26
star
25

rust-xcb-util

Rust bindings and wrappers for XCB utility functions.
Rust
26
star
26

ruby-mbox

A Ruby mbox parser.
Ruby
23
star
27

steamy

Steam controller handling.
Rust
21
star
28

ruby-threadpool

A simple no-wasted-resources thread pool implementation.
Ruby
21
star
29

ruby-x11

X11 bindings for Ruby
Ruby
20
star
30

wrong

The most wrong build system and package manager for C and C++.
Makefile
19
star
31

elixir-benchmark

Elixir benchmark utilities.
Elixir
18
star
32

mad

Managers & Developers
18
star
33

rust-mailbox

MBOX reader.
Rust
17
star
34

rust-hid

HID access library.
Rust
15
star
35

moc

Mirror of the moc project, with some of my own changes
C
15
star
36

rust-shmem

Shared memory for Rust.
Rust
14
star
37

ruby-clj

Like json, but with clojure sexps.
Ruby
14
star
38

NetCommander

NetCommander - An easy to use arp spoofing tool.
Python
14
star
39

miniLOL

Not just a CMS.
JavaScript
12
star
40

derp

Elixir error logger.
Elixir
12
star
41

continuum

Spaaaaceeeeeeeeeeeee.
Elixir
12
star
42

nucular

A reactor in D.
D
12
star
43

ruby-torchat

Torchat implementation in Ruby, event-driven EventMachine based library.
Ruby
12
star
44

rust-sixel

Sixel encoder/decoder for Rust.
Rust
11
star
45

ruby-ffi-inline

Inline C/C++ in Ruby easily and cleanly.
Ruby
11
star
46

rust-lzma

LZMA handling library.
Rust
10
star
47

fag

Forums Are Gay.
Ruby
10
star
48

clj.js

parse/dump Clojure like you'd do with JSON
JavaScript
10
star
49

vimmeh

Vim Script
9
star
50

rust-bdf

BDF format handling.
Rust
9
star
51

bitlbee-omegle

Omegle plugin for BitlBee.
C
9
star
52

lulzKey

lulzKey. Let's close those effin Windows so penguins won't enter.
C++
8
star
53

neovimmeh

Fennel
8
star
54

elixir-operators

Ever wanted operators which are as slow as can be? Now you can!
Elixir
8
star
55

arpoon

Man the harpoons, kill the spoofer!
Ruby
8
star
56

exts

Elixir Terms Storage, ets wrapper.
Elixir
8
star
57

failirc

Fail IRC library in Ruby.
Ruby
7
star
58

lulzhttpd

A C++ web server.
C++
7
star
59

screenruster-saver

Helper library to create screen savers.
Rust
7
star
60

rust-uinput-sys

Linux uinput definitions.
Rust
7
star
61

elixir-managed_process

Garbage collected processes.
Elixir
7
star
62

npapi-mumble-link

Mumble Link wrapped as an NPAPI plugin.
C
7
star
63

expo

XPosed for Kotlin.
Kotlin
7
star
64

opentyrian

opentyrian mirror with some of my own changes
C
7
star
65

rust-openal

OpenAL wrapper
Rust
6
star
66

rust-picto

Image handling library for Rust.
Rust
6
star
67

ruby-fuse

Bindings and wrapper for FUSE with FFI.
Ruby
6
star
68

shumei

Shumei brings an actor system and other concurrency plumbing primitives to the web.
TypeScript
6
star
69

random

Random codes.
Assembly
6
star
70

elixir-finalizer

Define finalizers using the cool NIF hack.
Elixir
6
star
71

httprot

Prot prot prot.
Elixir
6
star
72

screenruster-saver-laughing_man

I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
Rust
6
star
73

ruby-call-me

Various calling things, overload, pattern matching, memoization and such.
Ruby
6
star
74

orgasm

ORGanized ASseMbly: a Ruby (dis)?assembler library (NOT a (dis)?assembler FOR Ruby, a (dis)?assembler IN Ruby)
Ruby
6
star
75

llvm-lol

LOLCODE LLVM implementation.
C++
5
star
76

ruby-glyr

Ruby wrapper for glyr.
Ruby
5
star
77

decimex

erlang-decimal wrapper for Elixir
Elixir
5
star
78

nohomo

Homoglyphs begone!
Vim Script
5
star
79

clj-sockets

POSIX sockets for Clojure.
Clojure
5
star
80

rust-hwaddr

MAC address handling.
Rust
5
star
81

colorb

Colorify your strings
Ruby
5
star
82

rust-xkb

Rusty wrapper to libxkbcommon.
Rust
5
star
83

boolean-expression

A simple library to evaluate boolean expressions.
Ruby
5
star
84

bitlbee-torchat

Bitlbee plugin for using torchat.
C
5
star
85

amirite

Include and forget testing framework for C++11
C++
5
star
86

dexts

Disk Elixir Terms Storage, dest wrapper.
Elixir
5
star
87

nokku

Knock knock knock, hitori bocchi.
Rust
4
star
88

rust-xkbcommon-sys

Bindings to libxkbcommon.
Rust
4
star
89

screenruster-saver-hacks

ScreenRuster support for XScreenSaver hacks.
Rust
4
star
90

libcss

A library to parse and work with CSS
C
4
star
91

ela

Embedded Linear Algebra
C++
4
star
92

faildns

Fail DNS library in Ruby
Ruby
4
star
93

rust-curses-sys

Rust
3
star
94

ruby-clit

Simple library to make nice themeable CLI output.
Ruby
3
star
95

rft

Shitty Rust FFT implementation.
Rust
3
star
96

fagadget

New cross-toolkit and cross-platform desktop-gadget prototype.
C++
3
star
97

zwm

Shell
3
star
98

cryptsetup

Mirror of cryptsetup with some of my own changes
C
3
star
99

LOLastfm

LOL it's a scrobbler.
Ruby
3
star
100

parsepples

A parslet-like parser for C++
C++
3
star