title: How do I create a GraphQL subscription with Apollo Client in Vanilla JS
body: <p>Recently Apollo Client released a websocket subscription feature, but so far I've only seen it used by launching a query using subscribeToMore inside the componentWillMount lifecycle hook.</p>
  
  <p>Here is an example taken from <a href="https://dev-blog.apollodata.com/tutorial-graphql-subscriptions-client-side-40e185e4be76#0a8f" rel="noreferrer">https://dev-blog.apollodata.com/tutorial-graphql-subscriptions-client-side-40e185e4be76#0a8f</a></p>
  
  <pre class="lang-js prettyprint-override"><code>const messagesSubscription = gql`
    subscription messageAdded($channelId: ID!) {
      messageAdded(channelId: $channelId) {
        id
        text
      }
    }
  `
  
  componentWillMount() {
    this.props.data.subscribeToMore({
      document: messagesSubscription,
      variables: {
        channelId: this.props.match.params.channelId,
      },
      updateQuery: (prev, {subscriptionData}) =&gt; {
        if (!subscriptionData.data) {
          return prev;
        }
        const newMessage = subscriptionData.data.messageAdded;
        // don't double add the message
        if (!prev.channel.messages.find((msg) =&gt; msg.id === newMessage.id)) {
          return Object.assign({}, prev, {
            channel: Object.assign({}, prev.channel, {
              messages: [...prev.channel.messages, newMessage],
            })
          });
        } else {
          return prev;
        }
      }
    });
  }
  </code></pre>
  
  <p>But <a href="http://dev.apollodata.com/react/api-queries.html#graphql-query-data-subscribeToMore" rel="noreferrer" title="subscribeToMore">subscribeToMore</a> is specific to Apollo Client React integration. In <a href="http://dev.apollodata.com/core/apollo-client-api.html" rel="noreferrer" title="Vanilla JS">VanillaJS</a> there is a watchQuery, but it's stated it should not be used for subscriptions. There is also a <a href="http://dev.apollodata.com/core/apollo-client-api.html#ObservableQuery%5C.subscribe" rel="noreferrer" title="subscribe">subscribe</a> that might be what I'm searching for, but is not documented.</p>
  
  <p>Is there any way using Apollo GraphQL client to handle subscriptions, without being inside a React Component?</p>