mbari-org / lcm-websocket-server

WebSocket server for republishing LCM messages.
3 stars 0 forks source link

Ability to subscribe to specific channels #7

Closed carueda closed 1 year ago

carueda commented 1 year ago

As briefly discussed with @gtroni, it would be convenient for clients to have the ability to indicate the specific channels to subscribe to. A particular benefit is the potentially significant reduction of network traffic, when, for example, a "remote" user only wants to see positions on the compaswebui app.

kevinsbarnard commented 1 year ago

Hi @carueda, this is already possible. See the example in the README:

lcm-websocket-server --host localhost --port 8765 --channel ".*" your_lcm_types_package
kevinsbarnard commented 1 year ago

Ah, clients. Yes, that would be possible -- I would design it as a regex on the channel name, to be specified in the URL path.

kevinsbarnard commented 1 year ago

@carueda I just implemented this, so you can now subscribe with a regex in the path, e.g.:

http://localhost:8765/.*_SYNC

Built and pushed to Docker hub.

carueda commented 1 year ago

Thanks @kevinsbarnard !

See the example in the README:

 lcm-websocket-server --host localhost --port 8765 --channel ".*" your_lcm_types_package

Ha, I was not thinking about that approach when I entered this ticket, but it could actually be effective! Let me explain:

Make sense?

Now, to respond to the other approach you implemented, let me see if I understand:

From the point of view of compaswebui:

Is that what you have in mind?

Sorry I was not more specific when I entered this ticket but, regarding a request-based mechanism, I was thinking that the filtering could be specified either as part of the ws:// URL itself when opening the connection, or via a subsequent websocket message from client to server.

For compaswebui purposes, there's no need (at least in short term) for dynamic change of the filtering; only the one indicated at the start or very beginning of the connection.

kevinsbarnard commented 1 year ago

Ah, my bad @carueda! Both approaches are possible; I made an error in explaining what I just implemented.

  1. Filtering what the server subscribes to: this can be done from the docker-compose level by setting the CHANNEL environment variable. So, something like:

    lcm-websocket-server:
    container_name: lcm-websocket-server
    # Based on example provided by Kevin Barnard.
    image: mbari/lcm-websocket-server:compas-lcmtypes-0.1.0
    
    # Note: one of the following depending on whether on linux or macOS:
    network_mode: host
    #ports: ['8765:8765']
    
    environment:
    LCM_PACKAGES: compas_lcmtypes
    CHANNEL: FIRST_CHANNEL|SECOND_CHANNEL|THIRD_CHANNEL
  2. Filtering on the client subscribes to: I meant to put ws:// instead of http:// -- you can indeed specify the regex as part of the initial URL when opening the connection. So,
    ws://localhost:8765/FIRST_CHANNEL|SECOND_CHANNEL|THIRD_CHANNEL

    URLs are unquoted to handle special characters (e.g. %20 turns into a space)

carueda commented 1 year ago

Great! So, I think I'll start by using the ws:// approach mainly because it will make things simpler for the deployment creator (who can only focus on the associated platforms.yml, while not worrying about tweaking/repeating things in the docker-compose.yml), right @gtroni?