Giter Club home page Giter Club logo

gws's Introduction

Go Report Card GoDoc build codecov Mentioned in Awesome Go

gws

A WebSocket client and server for GraphQL.

gws implements Apollos' "GraphQL over WebSocket" protocol, which can be found in PROTOCOL.md. It provides an RPC-like API for both client and server usage.

Getting Started

gws provides both a client and server implementation for the "GraphQL over Websocket" protocol.

Client

To create a client, you must first dial the server like:

conn, err := gws.Dial(context.TODO(), "ws://example.com")
// Don't forget to handle the error

gws.Dial will return high-level abstraction around the underlying connection. This is very similar to gRPCs' behaviour. After dialing the server, now simply wrap the return conn and begin performing queries, like:

client := gws.NewClient(conn)

resp, err := client.Query(context.TODO(), &gws.Request{Query: "{ hello { world } }"})
// Don't forget to handle the error

var data struct{
  Hello struct{
    World string
  }
}
err = json.Unmarshal(resp.Data, &data)

Server

The server implementation provided isn't actually a standalone server but instead just a specially configured http.Handler. This is because the underlying protocol is simply WebSocket, which runs on top of HTTP. Creating a bare bones handler is as simple as the following:

msgHandler := func(s *Stream, req *Request) error {
  // Handle request
  return nil
}

http.ListenAndServe(":8080", gws.NewHandler(gws.HandlerFunc(msgHandler)))

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.