All Projects → rse → apollo-client-ws

rse / apollo-client-ws

Licence: other
GraphQL WebSocket Network Interface for Apollo Client

Programming Languages

javascript
184084 projects - #8 most used programming language
Makefile
30231 projects
HTML
75241 projects

Projects that are alternatives of or similar to apollo-client-ws

Subscriptions Transport Sse
A Server-Side-Events (SSE) client + server for GraphQL subscriptions
Stars: ✭ 55 (+323.08%)
Mutual labels:  apollo, transport
Graphql Ws
Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client.
Stars: ✭ 398 (+2961.54%)
Mutual labels:  apollo, transport
gofast
High performance transport protocol for distributed applications.
Stars: ✭ 19 (+46.15%)
Mutual labels:  transport
ultimate-hot-boilerplate
🚀 node-react universal app boilerplate with everything on hot reload, SSR, GraphQL, Flow included
Stars: ✭ 35 (+169.23%)
Mutual labels:  apollo
ctrip-apollo
The most delightful and handy Node.js client for ctrip apollo configuration service.
Stars: ✭ 56 (+330.77%)
Mutual labels:  apollo
react-apollo-form
Build React forms based on GraphQL APIs.
Stars: ✭ 195 (+1400%)
Mutual labels:  apollo
future-of-gql-servers
The future of GraphQL servers (GraphQL Europe 2018)
Stars: ✭ 27 (+107.69%)
Mutual labels:  apollo
Graphql Shield
🛡 A GraphQL tool to ease the creation of permission layer.
Stars: ✭ 3,121 (+23907.69%)
Mutual labels:  apollo
teamdaily
TeamDaily
Stars: ✭ 15 (+15.38%)
Mutual labels:  apollo
recipes-next-hasura
A lightweight app to explore and create recipes - Built with Next.js, Hasura, and Chakra-UI
Stars: ✭ 30 (+130.77%)
Mutual labels:  apollo
members
Managing People (Members), CRUD with Dashboard
Stars: ✭ 36 (+176.92%)
Mutual labels:  apollo
Apollo
Apollo is a Open-Source music player for playback and organization of audio files on Microsoft Windows, built using Python.
Stars: ✭ 13 (+0%)
Mutual labels:  apollo
vulcan-npm
The full-stack JavaScript App Framework
Stars: ✭ 26 (+100%)
Mutual labels:  apollo
waka
Your realtime guide to public transport in New Zealand
Stars: ✭ 37 (+184.62%)
Mutual labels:  transport
SpaceWar-ECS
A space war game made with ECS and JobSystem in Unity.
Stars: ✭ 26 (+100%)
Mutual labels:  transport
react-native-instagram-clone
Instagram Clone (light version) — Graphql + React (ios, android, web)
Stars: ✭ 29 (+123.08%)
Mutual labels:  apollo
Apollo 11
Original Apollo 11 Guidance Computer (AGC) source code for the command and lunar modules.
Stars: ✭ 52,190 (+401361.54%)
Mutual labels:  apollo
proper
A repository for the R tool propeR, which analyses travel time and cost using an OTP graph (see datasciencecampus/graphite)
Stars: ✭ 13 (+0%)
Mutual labels:  transport
hasura-node-monolith-example
Example of a monolithic web application using Hasura GraphQL Engine + Node.js + Next.js
Stars: ✭ 25 (+92.31%)
Mutual labels:  apollo
instagram-clone
Instagram clone using apollo, react and graphQl (hasura-graphQl engine)
Stars: ✭ 44 (+238.46%)
Mutual labels:  apollo

Apollo-Client-WS

GraphQL WebSocket Network Link for Apollo Client

About

This is a GraphQL WebSocket based ApolloLink layer for the JavaScript GraphQL client library Apollo Client. It was developed for and is intended to be used with the HAPI server framework and its seamless WebSocket protocol integration module HAPI-Plugin-WebSocket, although it could be used with any server speaking GraphQL over a framed WebSocket communication. Apollo-Client-WS has deferred connection establishment, connection keepalive support and can reconnect to the server automatically. Additionally, beside the GraphQL request/response messages, it also allows the application to send/receive arbitrary messages over the WebSocket connection, too.

Installation

$ npm install graphql-tag apollo-client apollo-client-ws apollo-link

Usage

const gql                = require("graphql-tag")
const { ApolloClient }   = require("apollo-client")
const { ApolloClientWS } = require("apollo-client-ws")
const { InMemoryCache }  = require("apollo-cache-inmemory")

const link = new ApolloClientWS({
    uri: "ws://127.0.0.1:12345/api",
    opts: {
        /*  (all options and their default values)  */
        debug:             0,
        log:               (msg) => { console.log(msg) },
        protocols:         [],
        compress:          false,
        encoding:          "json",
        keepalive:         0,
        reconnectattempts: 10,
        reconnectdelay:    2 * 1000
    }
})

const client = new ApolloClient({
    link:  link,
    cache: new InMemoryCache()
})

client.query({ query: gql`{ ... }` })
    .then((response) => { ...  })
    .catch((err)     => { ...  })

Network Protocol

Apollo-Client-WS on the WebSocket connection speaks WebSocket-Framed, a very simple protocol based on the following frame format:

[ fid: number, rid: number, type: string, data: any ]

In particular, the following frames are used for the GraphQL requests and (their corresponding) responses:

request: [
    fid:  number = <fid>,
    rid:  number = 0,
    type: string = "GRAPHQL-REQUEST",
    data: { query: string, variables?: any, operationName?: string }
]

response: [
    fid:  number = <fid>,
    rid:  number = request.fid,
    type: string = "GRAPHQL-RESPONSE",
    data: { data?: any, error?: any[] }
]

When sending a custom message via ApolloClientWS::send(type: string, data: any), the following frame is sent:

message: [
    fid:  number = <fid>,
    rid:  number = 0,
    type: string = type,
    data: any    = data
]

When receiving such a custom frame, it is delivered via ApolloClientWS::on("receive", { type, data }) => { ... }).

Notice

There is also the alternative module Apollo-Link-WS and its underlying Subscriptions-Transport-WS for Apollo Client. In contrast to this module, Apollo-Client-WS intentionally has no direct built-in GraphQL subscription support. Also, Subscriptions-Transport-WS unfortunately, but by design, uses an opinionated way of implementing GraphQL subscriptions on the GraphQL engine side. The Apollo-Client-WS instead provides plain WebSocket communication, without any additional subscription protocol, and hence does not enfore any special support for subscriptions on the server side.

For implementing a GraphQL subscription or similar add-on protocol on top of Apollo-Client-WS, simply use the send method to send non-GraphQL request messages to the server and use the receive event for receiving non-GraphQL response messages from the server.

/*  send a subscribe command  */
let data = [ "foo", 42 ]
link.send("SUBSCRIBE", data)

/*  receive a notification command  */
link.on("receive", ({ type, data }) => {
    if (type === "NOTIFY")
        notify(...data)
})

For a more elaborate out-of-the-box solution to GraphQL query subscriptions, check out GraphQL-IO. Under the hood, it already uses Apollo Client and WebSocket-Framed.

License

Copyright (c) 2017-2021 Dr. Ralf S. Engelschall (http://engelschall.com/)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].