Giter Club home page Giter Club logo

wsify's Introduction

WSIFY

a tiny general purpose websocket server that could be used for building real-time apps in addition to giving you the power to simply accept/reject any websocket message when using the authorizer feature (a webhook that should respond with 200 OK on success and anything else to reject).

Philosophy

  • A websocket server should only responsible for transmitting messages in real-time between connected parties.
  • It isn't the websocket server responsibility to authorize whether a party is allowed to send certain message or not.
  • An authorizer must respond with 200 OK if a party can send a message.
    • 200 OK means "Authorized"
    • 5xx means "the authorizer is down, please close the current connection and reconnect"
    • anything else means "NotAuthorized"
  • The client identity should be declared in the query params while connecting to the websocket endpoint and as an argument in the args of the message if needed.

Definitions

Message

is a data structure contains some data to be transmitted.

{
  // command is a string describes what should be done
  // available commands are:
  // "join": joins a channel (specified in the "args.channel")
  // "leave": leaves a channel (specified in the "args.channel")
  // "broadcast": broadcasts a content (specified in the "args.content") to a channel (in "args.channel")
  "command":  "join",
  "args": {
    "channel": "some_channel"
  }
}

Authorizer

a webhook that responds with 200 OK to accept a message sent by a websocket client, the authorizer will receive a POST request containing a message data structure as described above, but there is one special command that isn't described which is "connect", it is fired before accepting the websocket connection and it sounds like

{
  "command": "connect",
  "args": {
    // array of all http headers sent by the client while trying to open a websocket connection.
    "headers": [/*...*/],
    // an object that contains all available query params sent by the client while trying to open a websocket connection.
    "query": {/*...*/}
  }
}

Usage

There is no need to say a lot on how to use this software, just connect using any websocket client to http://wsify:3000/ws and start sending messages

Examples

> How can a client/device connect to the websocket service?

by simply connecting to the following endpoint ws://your.wsify.service:port/ws

> What is the command used to join a channel named "hello"?

{
  "command":  "join",
  "args": {
    "channel": "hello"
  }
}

> What is the command used to broadcast a message to the channel "hello"?

{
  "command":  "broadcast",
  "args": {
    "channel": "hello"
  }
}

> How can I publish message to hello from another server without connecting to the websocket endpoint?

Do a post request to /broadcast with the following format:

{
    "channel": "hello",
    "args": {
      "content": "Hello World! from the server"
    }
}

for more info look at $ ./wsify --help

wsify's People

Contributors

alaaattya avatar alash3al avatar artyomlisovskij avatar bookvik avatar merzak7 avatar nezamy avatar ro-29 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

wsify's Issues

message limit

Anybody know Websocketify (wsify) v2.0
Per second exact how much message can handle??

Durability

It looks like there is no storage currently ?

Would be nice so that events not delivered do eventually get delivered.
Budget might be a good option as it also has raft these days

docker example (port 8080) not working

I was trying to follow the given examples, downloaded & started the docker version, and tried to connect with the recommneded chrome websocket client to ws://localhost:8080/subscribe but it failed every time. Please advise.

send on closed channel

goRoutineAction handle readjson, and readjson err will close chan.
after this time

if err := conn.WriteJSON(msg); err != nil {
    debug("A message cannot be published to (" + key + ") because of the following error (" + err.Error() + ")")
    closeCh <- true
}

send data on closed channel

feature request: ping / pong

for detecting broken connections we can use ping / pong mechanism.

If the WebSocket connection supports ping & pong (i.e. advertises itself as draft 01 or above), Channels will send ping messages to the client in order to verify that it is active. In protocol versions 5 and above, when using an old version of the WebSocket protocol, Channels will send pusher:ping event (see events to the client). The client should respond with a pusher:pong event.

source

go get -> go install

 > [3/4] RUN go get github.com/alash3al/wsify:                                                                                                                                                     
0.435 go: go.mod file not found in current directory or any parent directory.                                                                                                                      
0.435   'go get' is no longer supported outside a module.                                                                                                                                          
0.435   To build and install a command, use 'go install' with a version,                                                                                                                           
0.435   like 'go install example.com/cmd@latest'                                                                                                                                                   
0.435   For more information, see https://golang.org/doc/go-get-install-deprecation
0.435   or run 'go help get' or 'go help install'.

change go get to go install in Dockerfile
RUN go install github.com/alash3al/wsify@latest

how to use webhook?

when i run:wsify --events="", everything goes correct
but after i run:wsify, i can't connect to wsify anymore.

gometalinter

FYI I use gometalinter with following flags in my projects,

(https://github.com/alecthomas/gometalinter)

gometalinter --enable-all -D dupl -D lll -D gas -D goconst -D interfacer -D safesql -D test -D testify -D vetshadow\
 --tests --vendor --warn-unmatched-nolint --deadline=10m --concurrency=2 --enable-gc ./...

when I ran this on your project I see some issues, do you accept contributions? I can submit the PR

publish with websocket

publish with websocket, use http to publish is too slow , if i have thousands of message to publish that means i must send 1k http post to publish message. i use java/kotlin to develop , so i cant contribute to this idea.

Trouble using wsify

Hi, I wanted to try the project but I'm not having much success. I've downloaded the osx 64bit darwin binary and run it without webhooks like ./wsify_darwin_amd64 -events "". Now, when I open an instance of socket-io-tester and enter the address http://localhost:4040/subscribe I get periodic errors in the terminal:

{"time":"2018-03-27T18:13:30.993363+02:00","level":"ERROR","prefix":"echo","file":"echo.go","line":"285","message":"code=404, message=Not Found"}
{"time":"2018-03-27T18:13:31.762112+02:00","level":"ERROR","prefix":"echo","file":"echo.go","line":"285","message":"code=404, message=Not Found"}
{"time":"2018-03-27T18:13:34.296958+02:00","level":"ERROR","prefix":"echo","file":"echo.go","line":"285","message":"code=404, message=Not Found"}

I guess the errors might be related to the tester app requiring a specific handshake/protocol/whatever, so I changed to a simple script using python. I copied the client example from the python WebSockets documentation and replaced the typical hello world string with the JSON payload from your readme:

#!/usr/bin/env python3

import asyncio
import websockets

async def hello(uri):
    j = '{"payload": "hi from the terminal", "channel": "testchan"}'
    async with websockets.connect(uri) as websocket:
        await websocket.send(j)

asyncio.get_event_loop().run_until_complete(
    hello('ws://localhost:4040/subscribe'))

This time when I run the python script the wsify server crashes with:

$ ./wsify_darwin_amd64  -events ""
[*] Welcome to WSIFY 2.0
[*] Listening for connections on address :4040  ...
⇨ http server started on [::]:4040
panic: send on closed channel

goroutine 7 [running]:
main.WSHandler.func3(0xc4200b0640, 0x0, 0x0, 0xc42005a640, 0xc4200202a0)
	/home/alash3al/Dev/go/workspace/src/github.com/alash3al/wsify/server.go:92 +0x3f0
created by main.WSHandler
	/home/alash3al/Dev/go/workspace/src/github.com/alash3al/wsify/server.go:70 +0x2f4

Seeing the closed channel thing, maybe the problem is the script not waiting to receive any data. So looking at the client basic websocket example I modified the test script to include a newline character in the payload and wait to receiving data:

#!/usr/bin/env python3

import asyncio
import websockets

async def hello(uri):
    j = '{"payload": "hi from the terminal", "channel": "testchan"}\n'
    async with websockets.connect(uri) as websocket:
        await websocket.send(j)
        print("Sent payload")
        result = await websocket.recv()
        print("Received answer")

asyncio.get_event_loop().run_until_complete(
    hello('ws://localhost:4040/subscribe'))

This version however seems to hang after printing Sent payload and the server doesn't seem to reflect any activity. Any pointers on what I'm doing wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.