Skip to main content

Shared Protobuf event schemas for toGather microservices

Project description

togather-shared-events

This repository contains event schemas for the toGather event-driven backend system. We use Protocol Buffers (Protobuf) as the messaging format for events. It provides type-safe code generation for TypeScript and Python, enabling services in different languages to share a consistent event schema.

npm package npm downloads license

Table of Contents

  1. Directory Structure
  2. SDK Development
  3. Using Generated Code
  4. Best Practices
  5. Troubleshooting

Directory Structure

Protobuf files should follow a domain/version-based structure:

proto/
└── <domain>/
    └── <version>/
        └── <event>.proto
  1. Domain: The feature or module (e.g., user)
  2. Version: The event schema version (e.g., v1)

Example:

proto/user/v1/UserCreated.proto

This structure ensures forward-compatibility and makes schema evolution manageable.

SDK Development

Adding New Proto Files

  1. Place your .proto file in the appropriate proto/<domain>/<version> folder.
  2. Define the package in the .proto file to match the folder structure.

Example for user.create.v1:

syntax = "proto3";

package user.create.v1;

message UserCreated {
  string id = 1;
  string name = 2;
  string email = 3;
  int64 created_at = 4;
}

Linting

Lint your proto files to ensure consistency and adherence to standards:

pnpm buf:lint

This checks for things like missing packages, incorrect directory structure, and other style issues.

Code Generation

Generate TypeScript and Python code from your proto files:

pnpm buf:generate

The generated code is placed in the gen/ directory:

gen/ts/...
gen/python/...

Tip: Keep buf.gen.yaml updated if you add new languages or plugins.

Publishing Package

To publish the package, run the following command:

pnpm run release

This command performs the following steps:

  • Deletes the old build artifacts.
  • Lints the proto files.
  • Generates new code for TypeScript and Python.
  • Patches the npm version.
  • Publishes the package to the registry.

Note: Ensure all changes are committed and tested before running this command.

Using Generated Code

TypeScript

Dependencies:

pnpm add @bufbuild/protobuf@^2.9.0

Example usage:

import {UserCreated,UserCreatedSchema} from '@togatherlabs/event-sdk/user/v1'
import { create, toBinary, fromBinary, toJson } from '@bufbuild/protobuf';

// Create a new message
const newUser: UserCreated = create(UserCreatedSchema, {
  id: "1",
  name: "Abhiram",
  email: "abhiram.ars@gmail.com",
  createdAt: BigInt(Date.now()),
});

// Serialize to binary
const buffer = toBinary(UserCreatedSchema, newUser);

// Deserialize from binary
const decoded = fromBinary(UserCreatedSchema, buffer);

// Convert to JSON
const json = toJson(UserCreatedSchema, decoded);

Tip: Use toBinary/fromBinary for Kafka or network transmission. Use toJson/fromJson for logging or debugging.


Python

Install dependencies (for usage):

pip install protobuf togather-event-sdk 

Dependencies (for maintainers / publishing):

pip install build twine

Example usage:

from togather_event_sdk.user.v1 import UserCreated

# Create a new message
msg = UserCreated(
    id="1",
    name="Abhiram",
    email="abhiram@gmail.com",
    created_at=int(1728512345123),
)

# Serialize to bytes
binary_data = msg.SerializeToString()

# Deserialize from bytes
decoded = UserCreated()
decoded.ParseFromString(binary_data)

print(decoded)

Build & Publish Python SDK (for maintainers):

pnpm python:build

This runs:

  • buf generate
  • Adds init.py files
  • Builds the Python wheel + source tarball

Tip: Python type hints can be added with mypy-protobuf or community plugins for full static type checking.

Best Practices

  1. Package and folder alignment

    • package in .proto must match folder structure (proto/<domain>/<version>).
  2. Versioning

    • Always increment the version folder (v1, v2, etc.) when changing message formats to avoid breaking consumers.
  3. Type safety

    • Use the generated schemas rather than manually constructing messages.
    • In TypeScript, always use create(); in Python, use the generated UserCreated class.
  4. Schema evolution

    • Avoid renaming or deleting existing fields; mark them deprecated instead.
    • Use int64 for timestamps, string for IDs/emails, etc.

Troubleshooting

  • ModuleNotFoundError in Python

    • Ensure gen/ folder is in your PYTHONPATH.
    • Add __init__.py files in each subfolder if needed.
  • TypeScript errors with missing $typeName or UserCreated

    • Make sure you are importing the schema (UserCreatedSchema) and using create() instead of instantiating the type directly.
  • Buf generation issues

    • Run pnpm buf:lint first to catch package/directory mismatches.

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

togather_event_sdk-1.0.10.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

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

togather_event_sdk-1.0.10-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file togather_event_sdk-1.0.10.tar.gz.

File metadata

  • Download URL: togather_event_sdk-1.0.10.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.14

File hashes

Hashes for togather_event_sdk-1.0.10.tar.gz
Algorithm Hash digest
SHA256 5c27b41a6d8c3d17aba3c0f89656787432f4e4d8a2b8cd9878244576578df87a
MD5 c2a1039768432437ec6b0241cc677a8b
BLAKE2b-256 e47288f66e7d99f4a0704a3b5d59914c19386a0457f608c8dfe2696475faaddf

See more details on using hashes here.

File details

Details for the file togather_event_sdk-1.0.10-py3-none-any.whl.

File metadata

File hashes

Hashes for togather_event_sdk-1.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 f8346df7b53995be0e26742bf142724c96cde1d3b22a23ce02f6764c4a1e3e14
MD5 2892e22f691710eb1d47faaeee0b8af5
BLAKE2b-256 d61cb15b264cd640002716f68ce8bb821d122386f4f6e457c13b93ce70143bd6

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