Effects
Marble.js defines a common interface for many different kinds Effects. As you probably know, @marblejs/core defines a HttpEffect
type for dealing with HTTP request streams. In case of @marblejs/websockets, the module defines a WsEffect
which works within WebSocket protocol and deals with streams of Events.
Hello, world!
Lets start with typical hello world example. The very basic implementation of WebSocket Effect can look like in the code snipped below.
Like any other Effect, it is just a function which returns a stream of outgoing events. As we previously mentioned, in case of WebSocket protocol, we have to deal with Events instead of requests.
The Effect above responds to HELLO events with Hello, world!
message. In case of default WsEffect
interface, each incoming event has to be mapped to an outgoing event which is just an object with type
and payload
attributes.
Calculator
Lets do some cool math! In the next example we will try to build a very basic calculator using only streams! For the example purpose we will only need two Effects: the first that will match ADD events and the second that will match SUM events.
Cool, right?! 😎 Don't forget to include the add$
Effect in webSocketListener
.
Last updated