# pyfi-client

> Make PyFi functionality available to a client 📼

Latest version **1.0.4** (published 2018-05-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install pyfi-client
pnpm add pyfi-client
yarn add pyfi-client
bun add pyfi-client
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2018-05-24 |
| First published | 2018-04-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 11.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | pswoodworth |

## Links

- npm: https://www.npmjs.com/package/pyfi-client
- Repository: https://github.com/IDEO-coLAB/pyfi-client
- Homepage: https://github.com/IDEO-coLAB/pyfi-client#readme
- Issues: https://github.com/IDEO-coLAB/pyfi-client/issues
- npm.io page: https://npm.io/package/pyfi-client

## Dependencies (2)

- [randomstring](https://npm.io/package/randomstring.md) ^1.1.5
- [socket.io-client](https://npm.io/package/socket.io-client.md) ^2.0.4

## Recent versions

- 1.0.4 (latest) — 2018-05-24
- 1.0.3 — 2018-05-24
- 1.0.2 — 2018-04-08
- 1.0.1 — 2018-04-08
- 1.0.0 — 2018-04-04

## README

# pyfi-client [![CircleCI](https://circleci.com/gh/IDEO-coLAB/pyfi-client/tree/master.svg?style=shield&circle-token=:circle-token)](https://circleci.com/gh/IDEO-coLAB/pyfi-client/tree/master) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/IDEO-coLAB/pyfi-client/blob/master/LICENSE) [![npm](https://img.shields.io/npm/v/pyfi-client.svg)](https://www.npmjs.com/package/pyfi-client)

Quickly make Python functions available to a javascript client when used along with [PyFi](https://github.com/ideo-colab/pyfi) ✨

## Usage

Pyfi-Client duplicates the functionality exposed to the server and makes it available to the client via Socket.io.


### Setting up a server
Getting up and running is pretty simple. From your Node PyFi instance, attach a socket.io instance:
```js
py._.attachClientSocketIO(io)
```

Here's a full example using express:
```js
const express = require('express');
const PyFi = require('pyfi');

const app = require('express')();
const server = require('http').Server(app);
const io = require('socket.io')(server);

app.use(express.static('public'));


const py = PyFi({
    path: './python',
    imports: [{
      import: ['tell_me_the_time'],
      from: 'timing',
    }],
  });

py._.attachClientSocketIO(io);

// wait for PyFi to init
py._.onReady(() => {
  server.listen(3000, () => {
    console.log('listening on port 3000!');
  });
});
```
You can find the full working example [here](https://github.com/IDEO-coLAB/pyfi-client/tree/master/example).

### Connecting a Client

Once you have a server set up, it's quite straightforward to connect a client.

```js
const PyFiClient = require('pyfi-client');
// or
import PyFiClient from 'pyfi-client';

const py = PyFiClient(http://localhost:3000)

py._.onReady(()=>{
  py.tell_me_the_time()
  .then(result => {
    console.log(result)
  })
  .catch(error => {
    // handle error
  })
})
```
PyFiClient automatically duplicates all of the Python functions imported on the server on the client.

### Sending messages mid-execution
[Just like in PyFi](https://github.com/IDEO-coLAB/pyfi#sending-messages-through-pyfi-while-a-function-is-running), PyfiClient supports receiving messages from Python while a function is running. That allows for, for example, streaming status back to a client while a long-running function is in progress.

It looks very similar to PyFi on Node:
Python:
```py
def my_function():
  # ... do something ...
  pyfi_message('my message')
  # ... do something else ...
  return 'done!'
```
Client:
```js
py.my_function()
  .onMessage(data => {
    console.log(data)
    // 'my message'
  })
  .then(res => {
    console.log(res);
    // 'done'
  })
```


## Reference

### PyFiClient([address])
Returns a `PyFiClient` instance and initializes callables that match those available in the server instance of PyFi.

You may provide an address for the serer (i.e. `http://localhost:3000`), or if no server is provided, the client will connect to the host address. This matches the functionality of [socket.io](https://socket.io/).


### Methods
**`_.onReady(callback)`**
Attach a callback function to call when the instance of PyFiClient is ready.


## Contributing
We welcome issues and pull requests.

If you spot a bug, please provide as much context as possible – including your OS, which versions of Node and Python you're running on, how you're managing your Python environment, and anything else that could be relevant.

## License
MIT License (c) 2018 - Present IDEO CoLab

---
_Source: https://npm.io/package/pyfi-client · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
