npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-client-ws

v2.6.1

Published

GraphQL WebSocket Network Interface for Apollo Client

Downloads

162

Readme

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.