Getting Started

Installation

npm

npm i @bottlenose/rxsocketio --save

yarn

yarn add @bottlenose/rxsocketio

Open a two-way data stream using Socket.io

The most common use for WebSockets is to open a bi-directional data stream to publish messages to the server and receive messages back from it. The package provides a conduit operator for this purpose:

import { from } from 'rxjs';
import { conduit } from '@bottlenose/rxsocketio';

const messagesToSend$ = from([
  {body: 'data'},
  {body: 'more data'},
]);

// send messages over the WebSocket and receive messages back from it...
const socketResponse$ = messageToSend$.pipe(
  conduit({url: 'http://mysite.com'})
);
socketResponse$.subscribe(
  console.log, // log all messages received from the server
  console.error,
  () => console.log('Socket Closed!') 
);

Sending messages with a topic

By default, objects piped into the conduit operator are sent to the 'message' topic. You can simply add a topic key to the objects to send them to a particular topic:

Advanced options

You can also provide socketOptions, which will be passed into the Socket.io Manager:

Last updated

Was this helpful?