Typescript socket implementation backed by rxjs streams
npm install @obsidize/rx-socketbash
npm install --save @obsidize/rx-socket
`
- git:
`bash
npm install --save git+https://github.com/jospete/obsidize-rx-socket.git
`
Usage
$3
The below example uses a socket instance for both the client and the server.
However, when using this in your app you should only use this module as either the client or the server,
but not both (unless you like talking to yourself).
`typescript
import { RxJsonSocket } from '@obsidize/rx-socket';
import { delay, first, tap } from 'rxjs/operators';
const sendMessage = { message: 'hello' };
const receiveMessage = { message: 'world!' };
const client = new RxJsonSocket();
const server = new RxJsonSocket();
client.setBufferReceiveSource(server.bufferStream.onSend.pipe(
delay(10),
));
server.setBufferReceiveSource(client.bufferStream.onSend.pipe(
delay(10),
tap(() => server.emit(receiveMessage))
));
client.textStream.onSend.subscribe(v => console.log('client textStream.onSend: ' + v));
client.textStream.onReceive.subscribe(v => console.log('client textStream.onReceive: ' + v));
server.textStream.onSend.subscribe(v => console.log('server textStream.onSend: ' + v));
server.textStream.onReceive.subscribe(v => console.log('server textStream.onReceive: ' + v));
// The client and server sockets are "glued together" in such a way that we can
// send a message and wait for a response all in one go - much like an HTTP or websocket request.
const serverResponse = await client.send(sendMessage)
.pipe(first())
.toPromise();
console.log(serverResponse); // { message: 'world!' }
``