React Native SDK - sendCustomMessage method

Details about the sendCustomMessage method from OpenReplay's React Native SDK

React Native SDK ⁠-⁠ sendCustomMessage method

Sends a typed message to the native tracker. At the moment the only supported type is gql, which records GraphQL operations, for example from the @openreplay/tracker-graphql Relay middleware.

import OR from '@openreplay/react-native'

OR.sendCustomMessage('gql', JSON.stringify({
  operationKind: 'query',
  operationName: 'GetUser',
  variables: { id: '123' },
  response: { data: { user: { name: 'Demo User' } } },
  duration: 85,
}))

The same function is available as OR.tracker.sendMessage(type, msg) and as the named export sendMessage (import { sendMessage } from '@openreplay/react-native').

  • type: string: The message type (only 'gql' at the moment).
  • msg: string: JSON string with the message. For gql: operationKind, operationName, variables, response and duration.

Using it with the GraphQL Relay middleware

Section titled Using it with the GraphQL Relay middleware

@openreplay/tracker-graphql (which lists @openreplay/tracker as a peer dependency) expects a web tracker App. Pass it an object with active, timestamp and send, which forwards each GraphQL message to the React Native tracker:

import OR from '@openreplay/react-native'
import { createRelayMiddleware } from '@openreplay/tracker-graphql'

const appWrapper = {
  active: () => true,
  timestamp: () => Date.now(),
  send: (gqlMsg) => {
    const msg = JSON.stringify({
      operationKind: gqlMsg[1],
      operationName: gqlMsg[2],
      variables: gqlMsg[3],
      response: gqlMsg[4],
      duration: gqlMsg[5],
    })
    OR.sendCustomMessage('gql', msg)
  },
}

// createRelayMiddleware(sanitizer?) returns a function that takes the app
// @ts-ignore - emulating web tracker here for middleware
const middleware = createRelayMiddleware()(appWrapper)

// .. connect to relay network layer

This method doesn’t return any values.

If you have any questions about this process, feel free to reach out to us on our Slack .