Skip to main content

GRPC-MESSENGER

Image

PyPI - License PyPI - Python Version PyPI - Version Poetry Code style: black Statically typed: mypy


Send text messages over the network, using grpc.


Installation

python -m pip install grpc-messenger

Tech stack

How's

How it works in the background

When you create a backend context 2 threads are created. One for managing grpc clients (the ones used when you initiate a connection with someone else) and one for the grpc server (the one used by other to initiate a connection with you).

Inside each thread an asyncio event loop is running, both the grpc server and the grpc clients are asynchronous, for each connection made a bidirectional streaming is created.

Each application running is identified by it's server's address, so before the client connects to our server an identification process happens where we receive the direction of the client's server, and authenticate that the server provided really belongs to that client.

Once authenticated the client can start the bidirectional streaming.

How to use with a gui/cli library

The objective of this library is to be a backend/core/module for a front-end gui/cli application.

The basic

import grpc_messenger

# Create an class inheriting from ViewUpdate protocol
class MyView(grpc_messenger.ViewUpdate):
   ...

The requiere methods are:

  • connecting. Called when either you try to connect with someone else or someone else tries to connect with you
  • connected. Called when the connection described before is successful
  • failed. Called when the connection described before failed
  • new_message. Called when you receive a message from someone
  • disconnected. Called when a disconnection happens

Thread safe gui/cli library

interface = MyView()
with Backend("[::1]:50051", interface, thread_safe_view=True) as b:
    if b is not None:
        interface.start() # The method that starts your gui/cli library

this will call the methods described before immediately (from the background threads)

NOT thread safe gui/cli library with access to the render loop

interface = MyView()
with Backend("[::1]:50051", interface) as b:
    if b is not None:
        while interface.running(): 
            interface.render_frame() 
            b.render()

this will keep all events (like receiving a new message) on a buffer and will only call the methods when you use the render() method from the background

Neither of the cases described before

You could probably hack your way around using one of the methods described before

Give orders

With the guide described before you can see things that happen, now you need to know how to make things happen.

When you initiate the context for the backend it returns either None or a BackendI.

  • None. When the server could not be initiated, probably because the interface and port you selected for your server is already in use.
  • BackendI. When everything is fine.

The BackendI provides the following methods:

  • render. To render from the buffer only when you indicate that thread_safe_view=False (the default)
  • connect. To who you want to initiate a connection
  • send_message. To who you want to send a message and the message itself

Bind the orders to callbacks inside your gui/cli

class MyView(ViewUpdate):
    backend: BackendI
interface = MyView()
with Backend(address, interface) as b:
    if b is not None:
        interface.backend = b

this might allow you to do something like

class MyView(ViewUpdate):
    def on_press_button(self):
        self.backend.send_message("[::1]:50051", "hello")

This should be safe because when you start the view you also set the backend

Metadata

Release files for grpc-messenger 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grpc-messenger 1.0.1
File Size Uploaded
grpc_messenger-1.0.1.tar.gz 11.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for grpc-messenger 1.0.1
File Interpreter ABI Platform
grpc_messenger-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 24.1 kB

Release files / grpc_messenger-1.0.1.tar.gz

Download URL grpc_messenger-1.0.1.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b2a085d6b2e04367fded04b7cd68e25276a144ac12aa94f2bc346065b20cd4b7
BLAKE2b-256 checksum
How to use checksums
062a35f86f87232c6289b58a3d3be3b99f6d5550bbe3e53b91bfea0e32aaeb05
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.5 Windows/11

Release files / grpc_messenger-1.0.1-py3-none-any.whl

Download URL grpc_messenger-1.0.1-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0ce997b0eaf5c564120c7fb6a06b25011319b644868b4cbd856853f6a4ff547e
BLAKE2b-256 checksum
How to use checksums
fda66abdc70948f2363baf11584b817fb2ecd6997af680525f554c7ea569679d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.12.5 Windows/11

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page