# Custom Services

This example shows how to create a dai.RemoteConnection and register a custom Visualizer service callback. It starts a simple
named service (myService) that returns a fixed response when called remotely.

This example requires the DepthAI v3 API, see [installation instructions](https://docs.luxonis.com/software-v3/depthai.md).

## Pipeline

## Source code

#### Python

```python
#!/usr/bin/env python3
import depthai as dai
import signal
import sys
import time

isRunning = True
def stop():
    global isRunning
    isRunning = False

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!')
    stop()
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)

remoteConnection = dai.RemoteConnection()

def testService(input):
    print("Test service called with input:", input)
    return {"result": "testService result"}

remoteConnection.registerService("myService", testService)
while isRunning:
    time.sleep(1)
```

#### C++

```cpp
#include <atomic>
#include <chrono>
#include <csignal>
#include <iostream>
#include <memory>
#include <thread>

#include "depthai/depthai.hpp"
#include "depthai/remote_connection/RemoteConnection.hpp"

// Global flag for graceful shutdown
std::atomic<bool> quitEvent(false);

// Signal handler
void signalHandler(int signum) {
    std::cout << "You pressed Ctrl+C!" << std::endl;
    quitEvent = true;
}

// Test service function
nlohmann::json testService(const nlohmann::json& input) {
    std::cout << "Test service called with input: " << input.dump() << std::endl;
    return {{"result", "testService result"}};
}

int main() {
    // Set up signal handlers
    signal(SIGTERM, signalHandler);
    signal(SIGINT, signalHandler);

    try {
        // Create remote connection
        dai::RemoteConnection remoteConnection;

        // Register service
        remoteConnection.registerService("myService", testService);

        // Main loop
        while(!quitEvent) {
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }

    } catch(const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}
```

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
