graphql-boilerplates / react-fullstack-graphql

Starter projects for fullstack applications based on React & GraphQL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can get rid of http-link if and use web sockets instead

manjula-dube opened this issue · comments

There will be no overhead of http connections

import React, { Component } from "react";
import "./App.css";
import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { ApolloProvider } from "react-apollo";
import App from "./App";
import { SubscriptionClient } from "subscriptions-transport-ws";
import { WebSocketLink } from "apollo-link-ws";

const wsClient = new SubscriptionClient("ws://localhost:8000/subscriptions", {
  reconnect: true
});

// Create a WebSocket link:
const wsLink = new WebSocketLink(wsClient);

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

class App extends Component {
  render() {
    return (
      <ApolloProvider client={client}>
        <App />
      </ApolloProvider>
    );
  }
}

export default App;

commented

That's assuming that everyone will use websockets for every "endpoint". You really only need websockets for a few queries in an app (notifications, comments, etc) but the majority of the app, you'll just want a standard http link for things like posts/articles, users, etc. You can add a websocket link, along with other links (auth & http) to ApolloClient.

@iamclaytonray yes thats an assumption though.
You can add a websocket link, along with other links (auth & http) to ApolloClient.
Yes I know that. But web socket connection seem to be faster

commented

I think you misunderstood me. If you remove a standard http link and only have a websocket link on the client, the client will only take data from a websocket. That forces you to only use websockets. It's not an assumption. The assumption I was referring to was that everyone would use websockets for their entire application, which may seem faster, but puts additional load on servers. Even in Lambda/Azure/etc, this is not ideal. You only want to bring live data for a few things in an app

Gotcha :) I misunderstood ;)

commented

@manjula91 - Tone is often hard to tell in text and re-reading what I wrote, I saw that it could be perceived that I was being really hard. Just wanted to let you know that I was speaking in a gentle tone and definitely didn't want to make you feel bad about the proposal or our short convo. If what I said was biting, I am sorry! 😄

(I just need to use more emojis, haha)

Hi, thanks for the suggestion but we definitely recommend using HTTP when possible, since it's easier to scale out!