Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import io from 'socket.io-client';
import getBaseUrl from '../../../utils/get-base-url';
let mSocket: null | SocketIOClient.Socket = null;
let mIsConnecting = false;
/**
* Create io.socket if it doesn't exist. If it does, and it's disconnected
* then reconnect it. If io.socket is in process of connecting, this will
* resolve when the connection establishes.
*
* Because sails.io.js waits for first tick, by wrapping in a setTimeout,
* then it deletes io.socket, the eager io.socket exists.
* so i wrap setTimeout here in case this is called during first tick.
* see - https://github.com/balderdashy/sails.io.js/blob/88b96ea019db4a03a1fb5fff0600dc7fd23f7862/sails.io.js#L1639
*
* @param {string} [initialSIDCookie]
*
* @returns {Promise<void>}
*/
export function connectSocket(initialSIDCookie?: string): Promise<void> {
return new Promise((resolve, reject) => {
if (!mSocket) {
mIsConnecting = true;
mSocket = io(getBaseUrl(), {
forceNew: false,
transports: ["websocket"],
reconnection: false,
query: {
nosession: 'true',
__sails_io_sdk_version: '1.2.1',
__sails_io_sdk_platform: 'node',
__sails_io_sdk_language: 'javascript'
},
// query: 'nosession=true&__sails_io_sdk_version=1.2.1&__sails_io_sdk_platform=node&__sails_io_sdk_language=javascript',
...(!initialSIDCookie ? {} : {
extraHeaders: {
cookie: initialSIDCookie
}
})
});
} else if (isSocketConnected()) {
resolve();
return;
} else if (!isSocketConnecting()) {
mSocket!.connect();
}
// else is connecting
function handleError() {
mIsConnecting = false;
removeHandlers();
reject();
}
function handleConnect() {
mIsConnecting = false;
removeHandlers();
resolve();
}
function removeHandlers() {
mSocket!.off('error', handleError);
mSocket!.off('connect', handleConnect);
}
mSocket.on('error', handleError);
mSocket.on('connect', handleConnect);
});
}
/**
* If socket is not connected this does nothing. If it
* is connected, this will disconnect it and resolve
* once disconnection has completed.
*
* @returns Promise<void>
*/
export async function disconnectSocket() {
if (!mSocket) {
return;
} else if (isSocketConnecting()) {
try {
await connectSocket();
} catch(ignore) {
return;
}
} else if (!isSocketConnected()) {
return;
}
return new Promise(resolve => {
mSocket!.on('disconnect', function resolver() {
mSocket!.off('disconnect', resolver);
resolve();
});
mSocket!.disconnect();
});
}
export function isSocketConnecting() {
return mIsConnecting;
}
export function getSocket() {
return mSocket;
}
export function isSocketConnected() {
return !mSocket ? false : mSocket.connected;
}
export function emitToSailsSocket(
url: string,
{
method = 'get',
data,
headers
}: {
method?: 'get' | 'post' | 'put' | 'patch' | 'delete',
data?: object,
headers?: object
} = {}
): Promise<{
statusCode: number,
body: object,
headers?: object
}> {
return new Promise((resolve, reject) => {
if (!mSocket) {
reject('socket not initialized');
} else if (!isSocketConnected()) {
reject('socket is not connected');
} else {
mSocket!.emit(method, { url, data, headers }, resolve)
}
});
}
@Noitidart

This comment has been minimized.

Copy link
Owner Author

commented Sep 9, 2019

Here - balderdashy/sails#4733 (comment) - I explain why I used this over sails.io.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.