Connect to the websocket in order to receive events.
wss://trade.csgoempire.com/s/?EIO=3&transport=websocket
Connecting to the socket requires Socket.IO v4.x client
const { io } = require('socket.io-client');
const axios = require('axios');
// Replace "YOUR API KEY HERE" with your API key
const csgoempireApiKey = "YOUR API KEY HERE";
// Replace domain to '.gg' if '.com' is blocked
const domain = "csgoempire.com"
const socketEndpoint = `wss://trade.${domain}/trade`;
// Set the authorization header for all axios requests to the CSGOEmpire API Key
axios.defaults.headers.common['Authorization'] = `Bearer ${csgoempireApiKey}`;
let userData = null;
let userDataRefreshedAt = null;
async function refreshUserData() {
if (userDataRefreshedAt && userDataRefreshedAt > Date.now() - 15*1000) {
// refreshed less than 15s ago, should be still valid
return;
}
try {
// Get the user data from the socket
// Token is valid 30s
userData = (await axios.get(`https://${domain}/api/v2/metadata/socket`)).data;
userDataRefreshedAt = Date.now();
} catch (error) {
console.log(`Failed to refresh user data: ${error.message}`);
}
}
// Function for connecting to the web socket
async function initSocket() {
console.log("Connecting to websocket...");
try {
await refreshUserData();
// Initalize socket connection
const socket = io(
socketEndpoint,
{
transports: ["websocket"],
path: "/s/",
secure: true,
rejectUnauthorized: false,
reconnect: true,
query: {
uid: userData.user.id,
token: userData.socket_token,
},
extraHeaders: { 'User-agent': `${userData.user.id} API Bot` } //this lets the server know that this is a bot
}
);
socket.on('connect', async () => {
// Log when connected
console.log(`Connected to websocket`);
// Handle the Init event
socket.on('init', async (data) => {
if (data && data.authenticated) {
console.log(`Successfully authenticated as ${data.name}`);
// Emit the default filters to ensure we receive events
socket.emit('filters', {
price_max: 9999999
});
} else {
await refreshUserData();
// When the server asks for it, emit the data we got earlier to the socket to identify this client as the user
socket.emit('identify', {
uid: userData.user.id,
model: userData.user,
authorizationToken: userData.socket_token,
signature: userData.socket_signature
});
}
})
// Listen for the following event to be emitted by the socket after we've identified the user
socket.on('timesync', (data) => console.log(`Timesync: ${JSON.stringify(data)}`));
socket.on('new_item', (data) => console.log(`new_item: ${JSON.stringify(data)}`));
socket.on('updated_item', (data) => console.log(`updated_item: ${JSON.stringify(data)}`));
socket.on('auction_update', (data) => console.log(`auction_update: ${JSON.stringify(data)}`));
socket.on('deleted_item', (data) => console.log(`deleted_item: ${JSON.stringify(data)}`));
socket.on('trade_status', (data) => console.log(`trade_status: ${JSON.stringify(data)}`));
socket.on("disconnect", (reason) => console.log(`Socket disconnected: ${reason}`));
});
// Listen for the following event to be emitted by the socket in error cases
socket.on("close", (reason) => console.log(`Socket closed: ${reason}`));
socket.on('error', (data) => console.log(`WS Error: ${data}`));
socket.on('connect_error', (data) => console.log(`Connect Error: ${data}`));
} catch (e) {
console.log(`Error while initializing the Socket. Error: ${e}`);
}
};
initSocket();