Websocket Server
All the application code here is available from the docs git repository.
Example Websocket server application built on top of Tremor and meant to be a demonstration of linked transports.
Setup
All the code here is available in the git repository as well.
Sources
We configure a websocket onramp listening on port 8139:
- id: ws
type: ws
linked: true
codec: string
preprocessors:
- lines
config:
host: 0.0.0.0
port: 8139
Message flow
Incoming websocket messages from a client's websocket connection are sent to the pipeline echo
and the output of it is sent back again from the same connection.
binding:
- id: main
links:
"/onramp/ws/{instance}/out": ["/pipeline/echo/{instance}/in"]
"/pipeline/echo/{instance}/out": ["/onramp/ws/{instance}/in"]
Processing logic
Implementation for the echo
pipeline (techincally, echo with one twist):
define script process
script
match event of
# snot is a badger
case "snot" => "badger"
default => event
end
end;
create script process;
# main request processing
select event from in into process;
select event from process into out;
# tremor runtime errors from the processing script
select event from process/err into err;
Testing
Assuming you have all the code from the git repository, run the following to start our application:
docker-compose up
Test the websocket server with a tool like websocat.
Can be installed via cargo install websocat
for the lazy/impatient amongst us
# anything you type and enter, will be echoed back
# except snot which begets badger
$ websocat ws://localhost:8139
hello
hello
world
world
snot
badger
goodbye
goodbye
If there's internal tremor error while processing the incoming message (eg: codec or preprocessor failure), the error should be bubbled up to the client. To test this out, change the codec in the onramp configuration to be json
from string
and send an malformed json input:
# after changing the onramp codec to json
$ echo "{" | websocat -n1 ws://localhost:8139
{"error":"SIMD JSON error: Syntax at character 0 ('{')","event_id":0,"source_id":"tremor://localhost/onramp/ws/01/in"}