Search…
WebSockets
@marblejs/websockets module implements the RFC 6455 WebSocket protocol, providing full-duplex communication channels over a single TCP connection.

1
$yarn add @marblejs/websockets @marblejs/core rxjs fp-ts Copied! # Bootstrapping Like httpListener __the WebSocket module defines a similar way of bootstrapping the app. webSocket.listener.ts 1 import { webSocketListener } from '@marblejs/websockets'; 2 3 const effects = [ 4 effect1$,
5
effect2$, 6 // ... 7 ]; 8 9 const middlewares = [ 10 middleware1$,
11
middleware2$, 12 // ... 13 ]; 14 15 export const listener = webSocketListener({ effects, middlewares }); Copied! To create WebSocket app instance, we can use createWebSocketServer, which is a wrapper around ws server creator. When created, it won't automatically start listening to given port and hostname until you call its awaited instance. index.ts 1 import { createWebSocketServer } from '@marblejs/websockets'; 2 import { IO } from 'fp-ts/lib/IO'; 3 import { listener } from './webSocket.listener'; 4 5 const webSocketServer = createWebSocketServer({ 6 options: { 7 port: 1337, 8 host: '127.0.0.1', 9 }, 10 listener, 11 }); 12 13 const main: IO<void> = async () => 14 await (await webSocketServer)(); 15 16 main(); Copied! If you are curious about other ways of server bootstrapping, reach out the HTTP Server events chapter. # Effects Marble.js defines a common interface for many different kinds Effects. In case of @marblejs/websockets, the module defines a WsEffect which works within WebSocket protocol and deals with streams of Events. The very basic implementation of WebSocket Effect can look like in the code snipped below. hello.effect.ts 1 import { matchEvent } from '@marblejs/core'; 2 import { WsEffect } from '@marblejs/websockets'; 3 import { mapTo } from 'rxjs/operators'; 4 5 export const hello$: WsEffect = event$=> 6 event$.pipe(
7
matchEvent('HELLO'),
8
mapTo({ type: 'HELLO', payload: 'Hello, world!' }),
9
);
Copied!
Like any other Effect, it is just a function which returns a stream of outgoing events. 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.
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.
calculator.effect.ts
1
import { matchEvent } from '@marblejs/core';
2
import { WsEffect } from '@marblejs/websockets';
3
import { t, eventValidator$} from '@marblejs/middleware-io'; 4 import { buffer, map } from 'rxjs/operators'; 5 6 export const sum$: WsEffect = event$=> 7 event$.pipe(
8
matchEvent('SUM'),
9
);
10
11
export const add$: WsEffect = (event$, ...args) =>
12
event$.pipe( 13 matchEvent('ADD'), 14 eventValidator$(t.number),
15
buffer(sum$(event$, ...args)),
16
map(events => events.reduce((a, e) => e.payload + a, 0)),
17
18
);
Copied!
In the example above we did a little bit of RxJS magic using buffer operator, which buffers the source Observable values until closing notifier emits (in this case sum\$). Additionally, to be sure that incoming ADD events are sent with payload of type number, we used @marblejs/middleware-io validator, which is able to infer payload type from defined schema.
Lets examine in steps how the server will respond to given equation:
$7 + 3 + 1$
1
// #1 sent to server
2
{
3
4
5
}
6
7
// #2 sent to server
8
{
9
10
11
}
12
13
// #3 sent to server
14
{
15
16
17
}
18
19
// #4 sent to server
20
{
21
"type": "SUM",
22
}
23
24
// #5 response from server
25
{
26
"type": "SUM_RESULT",
27