Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
@akanass/rx-socket-client
Advanced tools
Reconnectable websocket client, RxJS compliant, wrote in full Typescript | ES6 for client and browser side.
This library is an enhancement of RxJS WebSocketSubject to add more features and the native support of Node.js environment.
$ yarn add @akanass/rx-socket-client rxjs
or
$ npm install --save @akanass/rx-socket-client rxjs
Rx-Socket-Client is designed to be the simplest way possible to make web socket connections and calls.
It's fully Typescript
| ES6
written so you can import it :
import { webSocket } from '@akanass/rx-socket-client';
or use CommonJS
:
const webSocket = require('@akanass/rx-socket-client').webSocket;
Now, it's easy to perform a WS
calls:
const socket$ = webSocket('ws://127.0.0.1:1235');
// send message
socket$.send('my message from socket');
// emit message on specific event
socket$.emit('event', 'my message from socket for event');
// receive message from server with callback
socket$.on('event', data => console.log(data)); // will display received data in console if event is fired
// receive message from server with Observable
socket$.on$('event').subscribe(data => console.log(data)); // will display received data in console if event is fired
webSocket(urlConfigOrSource)
Returns an instance of RxSocketClientSubject
with given configuration.
Parameter:
{string | RxSocketClientConfig} urlConfigOrSource (required):
url
orRxSocketClientConfig
object with default values foreach next web socket calls
Result:
new
RxSocketClientSubject
instance
.connectionStatus$
This property provides an Observable to check server's connection status.
For example:
const socket$ = webSocket('ws://127.0.0.1:1235');
socket$.connectionStatus$.subscribe(isConnected => isConnected ? console.log('Server connected'): console.log('Server disconnected'));
.send(data)
This method sends data to web socket server.
Parameter:
{any} data (required): data sent to web socket server. Can be of any type.
Note: If data
is an object, it'll be stringified with JSON.stringify
. If it's a string or a buffer, it'll be sent like this without transformation.
Note: The message sent to server will be like this:
For binary data,
{
type: 'binary',
binaryData: `data`
}
For others,
{
type: 'utf8',
utf8Data: `data`
}
.emit(event, data)
This method emits data for given event to web socket server.
Parameters:
- {string} event (required): event sent to web socket server.
- {any} data (required): data sent to web socket server. Can be of any type.
Note: This method calls .send
method with object parameter {event, data}
.
.on(event, cb(data))
This method handles text response for given event from web socket server.
Parameters:
- {string | 'close'} event (required): event represents value inside
{utf8Data.event}
or{event}
from server response.- {function} cb (required): cb is the function executed if event matches the response from the server.
data
in parameter is the text data received from the server.
Note: close
event will be only fired by Observable
complete process.
Note: Message received from the server can be like this:
UTF Text Message,
{
type: 'utf8',
utf8Data: {
event: `event`,
data: `data`
}
}
Simple Text Message,
{
event: `event`,
data: `data`
}
For example:
const socket$ = webSocket('ws://127.0.0.1:1235');
// receive message from server with callback
socket$.on('event', data => console.log(data)); // will display received data in console if event is fired
// handle close from server with callback
socket$.on('close', () => console.log('Socket closed')); // will display message in console if event is fired
.on$(event)
This method is the same as .on
but with Observable
result.
Parameter:
{string} event (required): event represents value inside
{utf8Data.event}
or{event}
from server response.
Result:
Observable instance
Note: close
event is not supported with this method, check after for specific implementation. But, you can just use complete
section of each subscription to handle them in each event if you want.
For example:
const socket$ = webSocket('ws://127.0.0.1:1235');
socket$.on$('event').subscribe(data => console.log(data)); // will log data from server in console if event is fired
// handle close in subscription
socket$.on$('*').subscribe(undefined, undefined, () => console.log('Socket closed'));
.onBytes(cb(data))
This method handles binary response from web socket server.
Parameter:
{function} cb (required): cb is the function executed with the response from the server.
data
in parameter is the binary data received from the server.
Note: Binary received from the server can be like this:
Bytes Message,
{
type: 'binary',
binaryData: <Buffer 74 6f 74 6f>
}
Simple Bytes Message,
<Buffer 74 6f 74 6f>
For example:
const socket$ = webSocket('ws://127.0.0.1:1235');
socket$.onBytes(data => console.log(data)); // will log data from server in console if event is fired
.onBytes$()
This method is the same as .onBytes
but with Observable
result.
Result:
Observable instance
Note: close
event is not supported with this method, check after for specific implementation. But, you can just use complete
section of each subscription to handle them in each event if you want.
For example:
const socket$ = webSocket('ws://127.0.0.1:1235');
socket$.onBytes$().subscribe(data => console.log(data)); // will log data from server in console if event is fired
// handle close in subscription
socket$.onBytes$().subscribe(undefined, undefined, () => console.log('Socket closed'));
.onClose$()
This method handles close
from web socket server with Observable
result and send complete
inside next
process.
Result:
Observable instance
For example:
const socket$ = webSocket('ws://127.0.0.1:1235');
socket$.onClose$().subscribe(() => console.log('Socket closed')); // will log close from server in console if event is fired
RxSocketClientConfig
in detail
- {string} url (required): connection url to web socket server.
- {string | Array} protocol (optional): the protocol to use to connect.
- {'blob' | 'arraybuffer'} binaryType (optional): sets the
binaryType
property of the underlying WebSocket.- {number} reconnectInterval (optional): sets the reconnection interval value. (default:
5000 ms
).- {number} reconnectAttempts (optional): sets the reconnection attempts value. (default:
10
).- { { new(url: string, protocol?: string | Array): WebSocket } } WebSocketCtor (optional): a WebSocket constructor to use. This is useful for mocking a WebSocket for testing purposes.
To set up your development environment:
cd
to the main folder,npm or yarn install
,npm or yarn run test
.
./coverage/lcov-report/index.html
.rxjs:7.3.0
and delete incompatible packageses5
version and now module is only on es2015
and if you want an older support, your bundle system should transpile it to es5
error
process/methods because never called with reconnectionRx-Socket-Client
a drop-in replacement for WebSocketSubject
Copyright (c) 2018 Nicolas Jessel Licensed under the MIT license.
FAQs
Reconnectable websocket client with RxJS Subject
We found that @akanass/rx-socket-client demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.