API Reference
Client API Reference
Complete reference for the SOCKJS client API
1SockJS Constructor
javascript
new SockJS(url, _reserved, options)Parameters:
2Event: onopen
Fired when connection is established.
javascript
sock.onopen = function() {
console.log('Connected!');
};3Event: onmessage
Fired when a message is received.
javascript
sock.onmessage = function(e) {
console.log('Data:', e.data);
};4Event: onclose
Fired when connection is closed.
javascript
sock.onclose = function(e) {
console.log('Code:', e.code);
console.log('Reason:', e.reason);
};5Method: send(data)
Send data to the server.
javascript
sock.send('Hello Server');
sock.send(JSON.stringify({ type: 'message', content: 'Hi' }));6Method: close(code, reason)
Close the connection.
javascript
sock.close();
sock.close(1000, 'Normal closure');