0

I’ve got a standard client-server set-up with ReScript (ReasonML) on the front-end and a Python server on the back-end.

The user is running a separate process on localhost:2000 that I’m connecting to from the browser (UI). I can send requests to their server and receive responses.

Now I need to issue those requests from my back-end server, but cannot do so directly. I’m assuming I need some way of doing it through the browser, which can talk to localhost on the user’s computer.

  1. What are some conceptual ways to implement this (ideally with GraphQL)? Do I need to have a subscription or web sockets or something else?

  2. Are there any specific libraries you can recommend for this (perhaps as examples from other programming languages)?

Peteris
  • 3,548
  • 4
  • 28
  • 44

1 Answers1

1

I think the easiest solution with GraphQL would be to use Subscriptions indeed, the most common Rescript GraphQL clients already have such a feature, at least ReasonRelay, Reason Apollo Hooks and Reason-URQL have it.

tsnobip
  • 289
  • 2
  • 8
  • Can you elaborate on how the client would respond to the subscription? AFAIK subscriptions are one way from server to client, but in this scenario, we are effectively making an asynchronous request from the server to the client and inverting the server-client relationship. – Peteris Aug 31 '20 at 11:01
  • 1
    I guess the exact implementation would depend on the client library you'd use, but basically you'd either send a mutation in one of the parameter callbacks of the hook (onNext, onCompleted) depending on your use-case, or with Relay the component referencing the subscribed value would be automatically rerendered and you would trigger the mutation depending on the result of your subscription. – tsnobip Aug 31 '20 at 11:49
  • Got it, finally - I'd need to effectively suspend my execution on the server side between when the subscription is sent and the new mutation is received. Is there some approach/convention for this that you would know of? In native Python this would be a `yield` operation but this is different because the mutation wouldn't necessarily be coupled to where the `yield` happened -> it would come into the GraphQL endpoint. – Peteris Aug 31 '20 at 13:14
  • 1
    I've asked a follow up question here: https://stackoverflow.com/questions/63673027/how-to-send-messages-to-specific-suspended-worker-from-ui-javascript-to-python to get a complete solution. – Peteris Aug 31 '20 at 14:47