protobuf

package
v1.28.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var File_envelope_proto protoreflect.FileDescriptor

Functions

func Middleware

func Middleware() func(ctx *velaros.Context)

Middleware provides Protocol Buffers message format support for Velaros WebSocket connections. Define standard .proto schemas, generate Go code with protoc, and use your protobuf types directly with Velaros - no special wrapper types needed. The middleware handles envelope wrapping/unwrapping transparently.

Message Format

Internally, messages are wrapped in an envelope containing:

  • id: Message identifier for request/reply correlation
  • path: Routing path (gRPC-style "/package.Service/Method" or custom "/api/users/get")
  • data: Your serialized protobuf message bytes

The envelope is handled automatically - you only work with your .proto message types.

Usage

// Define your standard .proto schema (no Velaros-specific types needed)
// syntax = "proto3";
// message GetUserRequest { int64 user_id = 1; }
// message GetUserResponse { string name = 1; }

// Generate with: protoc --go_out=. user.proto

router := velaros.NewRouter()
router.Use(protobuf.Middleware())

router.Bind("/users/get", func(ctx *velaros.Context) {
    var req userpb.GetUserRequest
    ctx.ReceiveInto(&req)  // Works like standard protobuf

    user := getUser(req.UserId)
    ctx.Send(&userpb.GetUserResponse{
        Name:  user.Name,
        Email: user.Email,
    })
})

Client Integration

Your clients need to wrap protobuf messages in the Velaros envelope format. The envelope.proto schema is available in this package for client code generation.

JavaScript example:

// Load envelope.proto and your schemas
const Envelope = root.lookupType("velaros.protobuf.Envelope");
const GetUserRequest = root.lookupType("users.GetUserRequest");

// Create and encode your message
const request = GetUserRequest.create({ userId: 123 });
const requestBytes = GetUserRequest.encode(request).finish();

// Wrap in envelope
const envelope = Envelope.create({
    id: "msg-123",
    path: "/users/get",
    data: requestBytes
});

// Send envelope
ws.send(Envelope.encode(envelope).finish());

The middleware validates the Sec-WebSocket-Protocol header, expecting "velaros-protobuf" if present.

Types

type Envelope

type Envelope struct {

	// id is the message identifier used for request/reply correlation.
	// Optional for one-way messages, required for request/reply patterns.
	Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
	// path is the message path used for routing to handlers.
	// Format can be gRPC-style ("/package.Service/Method") or custom ("/api/users/get").
	Path string `protobuf:"bytes,2,opt,name=path,proto3" json:"path,omitempty"`
	// data contains your serialized protocol buffer message.
	// This is the bytes from proto.Marshal(yourMessage).
	Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"`
	// meta contains optional metadata attached to the message.
	// Applications can use this to pass authentication tokens, tracing IDs,
	// or other contextual information alongside message data.
	Meta map[string][]byte `` /* 135-byte string literal not displayed */
	// contains filtered or unexported fields
}

Envelope wraps your protocol buffer messages with routing metadata. This is internal to the Velaros protobuf middleware - on the server side you work directly with your own .proto definitions without envelope awareness.

The envelope provides:

  • Message ID for request/reply correlation
  • Path for routing to handlers
  • Data field containing your serialized protobuf message
  • Meta field for optional metadata (authentication tokens, tracing IDs, etc.)

Define your own .proto schemas following the standard Protocol Buffers tutorial and use them directly with Velaros. The middleware automatically wraps and unwraps messages in this envelope format.

func (*Envelope) Descriptor deprecated

func (*Envelope) Descriptor() ([]byte, []int)

Deprecated: Use Envelope.ProtoReflect.Descriptor instead.

func (*Envelope) GetData

func (x *Envelope) GetData() []byte

func (*Envelope) GetId

func (x *Envelope) GetId() string

func (*Envelope) GetMeta added in v1.15.0

func (x *Envelope) GetMeta() map[string][]byte

func (*Envelope) GetPath

func (x *Envelope) GetPath() string

func (*Envelope) ProtoMessage

func (*Envelope) ProtoMessage()

func (*Envelope) ProtoReflect

func (x *Envelope) ProtoReflect() protoreflect.Message

func (*Envelope) Reset

func (x *Envelope) Reset()

func (*Envelope) String

func (x *Envelope) String() string

type TestRequest

type TestRequest struct {
	UserId int64  `protobuf:"varint,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
	Name   string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
	// contains filtered or unexported fields
}

TestRequest is a simple test message for middleware testing

func (*TestRequest) Descriptor deprecated

func (*TestRequest) Descriptor() ([]byte, []int)

Deprecated: Use TestRequest.ProtoReflect.Descriptor instead.

func (*TestRequest) GetName

func (x *TestRequest) GetName() string

func (*TestRequest) GetUserId

func (x *TestRequest) GetUserId() int64

func (*TestRequest) ProtoMessage

func (*TestRequest) ProtoMessage()

func (*TestRequest) ProtoReflect

func (x *TestRequest) ProtoReflect() protoreflect.Message

func (*TestRequest) Reset

func (x *TestRequest) Reset()

func (*TestRequest) String

func (x *TestRequest) String() string

type TestResponse

type TestResponse struct {
	Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"`
	Code    int32  `protobuf:"varint,2,opt,name=code,proto3" json:"code,omitempty"`
	// contains filtered or unexported fields
}

TestResponse is a simple test response message

func (*TestResponse) Descriptor deprecated

func (*TestResponse) Descriptor() ([]byte, []int)

Deprecated: Use TestResponse.ProtoReflect.Descriptor instead.

func (*TestResponse) GetCode

func (x *TestResponse) GetCode() int32

func (*TestResponse) GetMessage

func (x *TestResponse) GetMessage() string

func (*TestResponse) ProtoMessage

func (*TestResponse) ProtoMessage()

func (*TestResponse) ProtoReflect

func (x *TestResponse) ProtoReflect() protoreflect.Message

func (*TestResponse) Reset

func (x *TestResponse) Reset()

func (*TestResponse) String

func (x *TestResponse) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL