Websocket
#
InitializationWebSocketService allows you to create a websocket client. This example demonstrates how to use a websocket client and how to handle events.
import CLPhotos from 'web_photos';
let photosApp;
const options = { logLevel: 'error' // 'trace' | 'debug' | 'info' | 'warn' | 'error'};
const config = { authToken: '', // user auth token baseUrl: '', // api base url required userAgent: '' // user agent name required}
try { photosApp = CLPhotos.init(options, config);} catch (e) { // catch photo sdk InternalError}
import CLPhotos from 'web_photos';
let photosApp;
const options = { logLevel: 'error', // 'trace' | 'debug' | 'info' | 'warn' | 'error'};
const config = { authToken: '', // user auth token baseUrl: '', // api base url required userAgent: '', // user agent name required wsConfig: { sockjs: `${sockJsUrl}`, websocket_event_groups: ['photos'], },};
try { photosApp = CLPhotos.init(options, config);} catch (e) { // catch photo sdk InternalError}
// get websocket clientconst wsClient = photosApp.webSocketService.client();
// create event listenersfunction openHandler(event: Event) { console.log('open event', event);}
function messageHandler(message: MessageEvent<any>) { // WebSocketService.getMessageEventData method // helps to get data as object from message event // const messageData = WebSocketService.getMessageEventData(message); // IWSData console.log('message', messageData);}
function closeHandler(event: CloseEvent) { console.log('close event', event);}
// add event listenerswsClient.addEventListener('open', openHandler);wsClient.addEventListener('message', messageHandler);wsClient.addEventListener('close', closeHandler);
// remove all event listener// if you don't want to listen to websocket`s events anymorewsClient.removeEventListener('open', openHandler);wsClient.removeEventListener('message', messageHandler);wsClient.removeEventListener('close', closeHandler);
if you use method .client() with new config as a first parameter, old websocket connection will be replaced with new one.
const newConfig = { authToken: '', // user auth token baseUrl: '', // api base url required userAgent: '', // user agent name required wsConfig: { sockjs: '${sockJsUrl}', websocket_event_groups: ['photos', 'family'], },};const newWSClient = photosApp.webSocketService.client(newConfig);// then you can add event listeners to new newWSClient and get messages from websocket api