Your stable connection to ws server
const createPhoenix = require('phoenix');
const phoenix = createPhoenix(WebSocketClient, {
uri: 'ws://127.0.0.1/ws',
timeout: 500,
logger: console,
strategy: createPhoenix.strategies.powerOf2,
});
phoenix
.on('connected', () => {
// connected
})
.on('disconnected', () => {
// connected
})
.on('message', ({ data }) => {
// data is the sent message
});import createPhoenix from 'phoenix';
const phoenix = createPhoenix(WebSocket, {
uri: 'ws://127.0.0.1/ws',
timeout: 500,
logger: console,
strategy: createPhoenix.strategies.fibonacci,
});
phoenix
.on('connected', () => {
// connected
})
.on('disconnected', () => {
// connected
})
.on('message', ({ data }) => {
// data is the sent message
});const createPhoenix = require('phoenix');
const phoenix = createPhoenix(WSClient, options]);Arguments:
WSClient- the class that would be used for connection creation. Should be WebSocket or any other implementation with the same API. Required.optionsuri- remote WS server full url (e.g. wss://echo.websocket.org). Required.timeout- time span between reconnects. Optional. Default to0. Depends on selectedstrategy.logger- object that implements log interface (actually, 2 methods:logandwarn). Optional. If not passed - fallbacks toconsole. If there's no console - would not log anything. To disable logging set tonull.strategy- reconnect strategy. Optional. Default toconst. See full list of strategies below.
To stop reconnect from the server it shoud close the WS connection with code 4500.
Has no arguments. Drops the connection, removes all listeners, stops the reconnection if any active.
Sends a message to the connection. Returns true if connection to server is available, false otherwise. Returned true does not guarantee message to be sent.
Subscribes to the event from phoenix. See events for details. Returns the phoenix instance. Arguments:
eventName- name of the eventlistener- a callback function
Unsubscribes the given listener from the given event. If listener is omitted - all listeners for the given event would be unsubscribed. If both eventName and listener are omitted - all listeners would be unsubscribed. Returns the phoenix instance.
Arguments:
eventName- name of the eventlistener- a callback function
function onConnected() { }
Emitted every time the connection is up.
function onDisconnected() { }
Emitted every time the connection is down.
function onMessage({ data }) { }
Emitted when the client (phoenix) receives a message from server.
data- message from server
The default one. Takes the timeout option as a timspan between reconnects. Constant over time. E.g. timeout = 67, then sequence is 67, 67, 67, 67, ....
timeout option is ignored. Reconnect is scheduled based on fibonacci sequence in milliseconds. 0, 1, 1, 2, 3, 5, 8, 13, ....
Increases the timeout by 1ms on every reconnect starting with timeout option. E.g. timeout = 134, then sequence is 134, 135, 136, 137, ....
Takes 1ms as a start point and multiplies by 2 on every reconnect. 1, 2, 4, 8, 16, ....
Reconnect timeout is a random value in range [0, timeout).