Syft.js
Search…
API Documentation

Initialization

Syft Constructor

To create a new Syft instance, you instantiate it like such:
1
var syft = new Syft(options);
Copied!
You can supply the following options, all of which are optional and passed as an Object:
1
var options = {
2
url: 'ws://location-of-PySyft:8080/',
3
verbose: true
4
};
Copied!
Argument
Type
Description
url
string
The URL of the PySyft socket connection
verbose
boolean
Whether or not you want Syft.js to output all logs

start(url)

This method starts the connection to PySyft and sets up a socket listener to listen to incoming instructions. You may optionally define a url argument if you didn't in the Syft constructor documented above.
1
// With a URL being defined in the constructor
2
syft.start();
3
4
// With a URL being defined in start()
5
syft.start('ws://location-of-PySyft:8080/');
Copied!
This also sends two messages back to PySyft to let the server know the client is ready to receive tensors.

When the client is starting

1
{
2
"type": "socket-status",
3
"data": {
4
"status": "starting"
5
}
6
}
Copied!

When the client is ready for instructions

1
{
2
"type": "socket-status",
3
"data": {
4
"status": "ready"
5
}
6
}
Copied!

stop()

This method stops the connection to PySyft. It also clears the list of stored tensors in Syft.js.
1
syft.stop();
Copied!
This also sends back a single message to PySyft to let the server know the client has shut down.

When the client has stopped

1
{
2
"type": "socket-status",
3
"data": {
4
"status": "stopped"
5
}
6
}
Copied!

Functionality

addTensor(id, tensor)

This method adds a tensor to the list of stored tensors.
Argument
Type
Description
id
string
ID of the tensor
tensor
array
Value of the tensor
1
syft.addTensor('my-tensor', [[1, 2], [3, 4]]);
Copied!

Calling from PySyft

The request from PySyft:
1
{
2
"type": "add-tensor",
3
"id": "my-tensor",
4
"values": [[1, 2], [3, 4]]
5
}
Copied!
The response to sent back to PySyft:
1
{
2
"type": "add-tensor",
3
"data": {
4
"id": "my-tensor",
5
"tensor": CREATED_TENSORFLOW_TENSOR
6
}
7
}
Copied!

Return Value

This method returns a Promise:
1
syft.addTensor('my-tensor', [[1, 2], [3, 4]]).then(tensors => {
2
console.log('A list of all stored tensors', tensors);
3
});
Copied!

Event Handlers

This method triggers an event handler, onTensorAdded, that you may hook into.

removeTensor(id)

This method removes a tensor from the list of stored tensors.
Argument
Type
Description
id
string
ID of the tensor
1
syft.removeTensor('my-tensor');
Copied!

Calling from PySyft

The request from PySyft:
1
{
2
"type": "remove-tensor",
3
"id": "my-tensor",
4
}
Copied!
The response to sent back to PySyft:
1
{
2
"type": "remove-tensor",
3
"data": {
4
"id": "my-tensor"
5
}
6
}
Copied!

Return Value

This method returns a Promise:
1
syft.removeTensor('my-tensor').then(tensors => {
2
console.log('A list of all stored tensors', tensors);
3
});
Copied!

Event Handlers

This method triggers an event handler, onTensorRemoved, that you may hook into.

runOperation(func, tensors, result_id)

This method performs a TensorFlow.js operation on two tensors.
Argument
Type
Description
func
string
The TensorFlow.js operation ("add", "mul", etc.)
tensors
array
An array of two tensor ID's
result_id
string
An optional ID you can provide for the result
1
syft.runOperation('add', ['first-tensor', 'second-tensor'], "12345");
Copied!

Calling from PySyft

The request from PySyft:
1
{
2
"type": "run-operation",
3
"func": "add",
4
"tensors": ["first-tensor", "second-tensor"],
5
"result_id": "12345"
6
}
Copied!
The response to sent back to PySyft:
1
{
2
"type": "run-operation",
3
"data": {
4
"result": RESULT_OF_TENSORFLOW_OPERATION,
5
"tensors": [
6
FIRST_TENSORFLOW_TENSOR,
7
SECOND_TENSORFLOW_TENSOR
8
],
9
"result_id": "12345"
10
}
11
}
Copied!

Return Value

This method returns a Promise:
1
syft.runOperation('add', ['first-tensor', 'second-tensor']).then(result => {
2
console.log('A TensorFlow.js tensor', result);
3
});
Copied!

Event Handlers

This method triggers an event handler, onRunOperation, that you may hook into.

Event Handlers

onTensorAdded(data)

This event handler is triggered when a new tensor is added to the list of stored tensors. The arguments are returned as a single object, containing the following properties:
Argument
Type
Description
id
string
ID of the tensor
tensor
tensor
The TensorFlow.js tensor
tensors
array
The array of all tensors being stored
1
syft.onTensorAdded(({ id, tensor, tensors }) => {
2
console.log('The ID of the tensor', id);
3
console.log('The TensorFlow.js tensor', tensor);
4
console.log('A list of all stored tensors', tensors);
5
});
Copied!

onTensorRemoved(data)

This event handler is triggered when a tensor is removed from the list of stored tensors. The arguments are returned as a single object, containing the following properties:
Argument
Type
Description
id
string
ID of the tensor
tensors
array
The array of all tensors being stored
1
syft.onTensorRemoved(({ id, tensors }) => {
2
console.log('The ID of the tensor', id);
3
console.log('A list of all stored tensors', tensors);
4
});
Copied!

onRunOperation(data)

This event handler is triggered when an operation is run on two tensors. The arguments are returned as a single object, containing the following properties:
Argument
Type
Description
func
string
The TensorFlow.js operation ("add", "mul", etc.)
result
tensor
The result of the TensorFlow.js operation
1
syft.onRunOperation(({ func, result, result_id }) => {
2
console.log('The TensorFlow.js operation', func);
3
console.log('A TensorFlow.js tensor', result);
4
console.log('The optional ID of the resulting tensor', result_id);
5
});
Copied!

onMessageReceived(event)

This event handler is triggered when a message is received from PySyft.
Argument
Type
Description
event
object
The event object being received from
1
syft.onMessageReceived(event => {
2
console.log('The JSON passed to us from PySyft', event);
3
});
Copied!

onMessageSent(message)

This event handler is triggered when a message is sent back to PySyft. The arguments are returned as a single object, containing the following properties:
Argument
Type
Description
type
string
The type of message that was sent
data
depends on type
The data associated with the message
1
syft.onMessageSent(({ type, data }) => {
2
console.log('The type of message being passed', type);
3
console.log('The data being passed alongside', data);
4
});
Copied!

Helpers

getTensors()

This method returns all the tensors currently being stored by Syft.js.
1
syft.getTensors();
Copied!

Calling from PySyft

The request from PySyft:
1
{
2
"type": "get-tensors"
3
}
Copied!
The response to sent back to PySyft:
1
{
2
"type": "get-tensors",
3
"data": [
4
{
5
"id": "first-tensor",
6
"tensor": FIRST_TENSORFLOW_TENSOR
7
},
8
{
9
"id": "second-tensor",
10
"tensor": SECOND_TENSORFLOW_TENSOR
11
}
12
]
13
}
Copied!

Return Value

The return is an array of all stored tensors, it looks like such:
1
[
2
{
3
id: 'first-tensor',
4
tensor: FIRST_TENSORFLOW_TENSOR
5
},
6
{
7
id: 'second-tensor',
8
tensor: SECOND_TENSORFLOW_TENSOR
9
}
10
]
Copied!

getTensorById(id)

This method returns a tensor specified by an ID.
1
syft.getTensorById(id);
Copied!

Calling from PySyft

The request from PySyft:
1
{
2
"type": "get-tensor",
3
"id": "my-tensor"
4
}
Copied!
The response to sent back to PySyft:
1
{
2
"type": "get-tensor",
3
"data": {
4
"id": "my-tensor",
5
"tensor": REQUESTED_TENSORFLOW_TENSOR
6
}
7
}
Copied!

Return Value

The return is an object of the requested tensor, it looks like such:
1
{
2
id: 'my-tensor',
3
tensor: REQUESTED_TENSORFLOW_TENSOR
4
}
Copied!

getTensorIndex(id)

This method returns a tensor's index in the tensors array specified by an ID.
1
syft.getTensorIndex(id);
Copied!

Return Value

The return is a number representing the index in the tensors array, it looks like such:
1
1
Copied!