Skip to main content

The Python client/server package for ephaptic.

Project description

ephaptic logo

ephaptic


GitHub License GitHub Actions Workflow Status GitHub Actions Workflow Status GitHub Actions Workflow Status PyPI - Version NPM - Version

What is ephaptic?


ephaptic (adj.)
electrical conduction of a nerve impulse across an ephapse without the mediation of a neurotransmitter.

Nah, just kidding. It's an RPC framework.

ephaptic — Call your backend straight from your frontend. No JSON. Low latency. Invisible middleware.

Getting Started

  • Ephaptic is designed to be invisible. Write a function on the server, call it on the client. No extra boilerplate.

  • Plus, it's horizontally scalable with Redis (optional), and features extremely low latency thanks to msgpack.

  • Oh, and the client can also listen to events broadcasted by the server. No, like literally. You just need to add an eventListener. Did I mention? Events can be sent to specific targets, specific users - not just anyone online.

  • Saved the best for last: it's type-safe. Don't believe me? Try it out for yourself. Simply type hint return values and parameters on the backend, and watch those very Python types transform into interfaces and types on the TypeScript frontend. Plus, you can use Pydantic - which means, for those of you who are FastAPI users, this is going to be great.

What are you waiting for? Let's go.

To see why you might want to use Ephaptic instead of traditional REST, see the article on it.
Python
$ pip install ephaptic
from fastapi import FastAPI # or `from quart import Quart`
from ephaptic import Ephaptic

app = FastAPI() # or `app = Quart(__name__)`

ephaptic = Ephaptic.from_app(app) # Finds which framework you're using, and creates an ephaptic server.

You can also specify a custom path:

ephaptic = Ephaptic.from_app(app, path="/websocket")

And you can even use Redis for horizontal scaling!

ephaptic = Ephaptic.from_app(app, redis_url="redis://my-redis-container:6379/0")

Now, how do you expose your function to the frontend?

@ephaptic.expose
async def add(num1: int, num2: int) -> int:
    return num1 + num2
If you're trying to expose functions statelessly, e.g. in a different file, feel free to instead import and use the expose function from the library instead of the instance. Please note that if you do this, you must define all exposed functions before creating the ephaptic instance - easily done by simply placing your import line above the ephaptic constructor. The same thing can be done with the global identity_loader decorator.

Yep, it's really that simple.

But what if your code throws an error? No sweat, it just throws up on the frontend, with the error name.

And, want to say something to the frontend?

class Notification(BaseModel):
    message: str
    priority: Literal["high", "low", "default"]

await ephaptic.to(user1, user2).emit(Notification(message="Hello, world!", priority="high"))

To create a schema of your RPC endpoints:

$ ephaptic generate src.app:app -o schema.json # --watch to run in background and auto-reload on file change.
$ # Or:
$ ephaptic generate src.app:app -o ephaptic.d.ts # converts directly to typescript

Pydantic is entirely supported. It's validated for arguments, it's auto-serialized when you return a pydantic model, and your models receive type definitions in the schema.

To receive authentication objects and handle them:

from ephaptic import identity_loader

@identity_loader
async def load_identity(auth): # You can use synchronous functions here too.
    jwt = auth.get("token")
    if not jwt: return None # unauthorized
    ... # app logic to retrieve user ID
    return user_id

From here, you can use ephaptic.active_user() within any exposed function, and it will give you the current active user ID / whatever else your identity loading function returns. (This is also how ephaptic.to works.)

JavaScript/TypeScript — Browser (Svelte, React, Angular, Vite, etc.)

To use with a framework / Vite:

$ npm install @ephaptic/client

Then:

import { connect } from "@ephaptic/client";

const client = connect(); // Defaults to `/_ephaptic`.

Or, you can use it with a custom URL:

const client = connect({ url: '/ws' });
const client = connect({ url: 'wss://my-backend.deployment/ephaptic' });

You can even send auth objects to the server for identity loading.

const client = connect({ url: '...', auth: { token: window.localStorage.getItem('jwtToken') } })

And you can load types, too.

$ ephaptic from-schema ./schema.json -o schema.d.ts # --watch to auto-reload upon changes
import { connect } from "@ephaptic/client";
import { type EphapticService } from './schema';

const client = connect(...) as unknown as EphapticService;

Or, to use in your browser:

<script type="module">
import { connect } from 'https://cdn.jsdelivr.net/npm/@ephaptic/client@latest/+esm';

const client = connect(...);
</script>

See more in the docs.

License

MIT


© ephaptic 2025

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ephaptic-0.4.3.tar.gz (27.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ephaptic-0.4.3-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

File details

Details for the file ephaptic-0.4.3.tar.gz.

File metadata

  • Download URL: ephaptic-0.4.3.tar.gz
  • Upload date:
  • Size: 27.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for ephaptic-0.4.3.tar.gz
Algorithm Hash digest
SHA256 d09ae1b688db9d8929692ffca3de26f71eef0c3487df422f34c3ff06290ef1cb
MD5 88453821bfab5c4958cd04e42e6eba06
BLAKE2b-256 5396b7b8b6d0fe2e577777627f7bf7a43f70fc075161d9de8bac75d23efd9901

See more details on using hashes here.

File details

Details for the file ephaptic-0.4.3-py3-none-any.whl.

File metadata

  • Download URL: ephaptic-0.4.3-py3-none-any.whl
  • Upload date:
  • Size: 29.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for ephaptic-0.4.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7b603f4f0f112ed5c906f295afa4e0d8bff049fdac0dcf2fc7b0af4e69889864
MD5 397344222f668c7ce6c2b3778e513333
BLAKE2b-256 1f4848ed5d1a271ca3bd8d4767ff1ad4304d2b92f364ef7822ba4105af1a477e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page