Match via WebSocket
Match via WebSocket
Match information available through the Match API endpoint, automatically updated and delivered to your client systems in real-time through WebSocket protocol.
Send a Subscription Request
You can subscribe to the matches individually to receive automatic updates using the Match Subscribe API. The Sample snippet below demonstrates how to send a subscription request from the Match Subscribe API. Please note that subscribing twice to the same match does not mean that you will be charged twice, or will receive the same update twice. Additionally, please be aware that the maximum number of allowed connections per project via WebSocket would be 20.
Match Subscribe - Websocket
TERMINAL
pip install requests
pip install requests
Python
import requests
project_key = 'YOUR_PROJ_KEY'
token = 'YOUR_ACCESS_TOKEN'
match_key = 'ausind_2020_odi01'
url = "https://api.sports.roanuz.com/v5/cricket/{}/match/{}/subscribe/".format(project_key,match_key)
headers = {
'rs-token': token
}
payload = {
'method': "web_socket"
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
import requests
project_key = 'YOUR_PROJ_KEY'
token = 'YOUR_ACCESS_TOKEN'
match_key = 'ausind_2020_odi01'
url = "https://api.sports.roanuz.com/v5/cricket/{}/match/{}/subscribe/".format(project_key,match_key)
headers = {
'rs-token': token
}
payload = {
'method': "web_socket"
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
Handling the data received
The Sample snippet below demonstrates how to handle the data received through a simple WebSocket client.
WebSocket
// client.js
var io = require('socket.io-client')
// connect to roanuz sports server
var socket = io.connect('http://socket.sports.roanuz.com/cricket', {path:'/v5/websocket', reconnect: true})
// Add a connect listener
socket.on('connect', function (socket) {
console.log('Connected!')
})
// prepare required data to access the match data
var data = {}
data.token = 'YOUR_ACCESS_TOKEN' // token
data.match_key = 'MATCH_KEY' // match key
// emit signal to connect to the room
socket.emit('connect_to_match', data)
// server emits 'on_match_joined'
socket.on('on_match_joined', function (data) {
console.log('Cricket match joined!', data.key)
})
// the subscribed match content are emitted with 'on_match_update' event
// since it's gzipped data, parse it to unzip it
socket.on('on_match_update', function (res) {
console.log('data received')
console.log(JSON.parse(res))
})
// emits 'on_error' on,
//match not subscribed
socket.on('on_error', function (data) {
console.log('not_subscribed', JSON.parse(data))
})
// client.js
var io = require('socket.io-client')
// connect to roanuz sports server
var socket = io.connect('http://socket.sports.roanuz.com/cricket', {path:'/v5/websocket', reconnect: true})
// Add a connect listener
socket.on('connect', function (socket) {
console.log('Connected!')
})
// prepare required data to access the match data
var data = {}
data.token = 'YOUR_ACCESS_TOKEN' // token
data.match_key = 'MATCH_KEY' // match key
// emit signal to connect to the room
socket.emit('connect_to_match', data)
// server emits 'on_match_joined'
socket.on('on_match_joined', function (data) {
console.log('Cricket match joined!', data.key)
})
// the subscribed match content are emitted with 'on_match_update' event
// since it's gzipped data, parse it to unzip it
socket.on('on_match_update', function (res) {
console.log('data received')
console.log(JSON.parse(res))
})
// emits 'on_error' on,
//match not subscribed
socket.on('on_error', function (data) {
console.log('not_subscribed', JSON.parse(data))
})
Unsubscribing from a match
For any reasons, if you wish to opt-out of receiving automatic updates for a match, you can do so by placing an unsubscribe request from the Match Unsubscribe API.
Match Unsubscribe - Websocket
TERMINAL
pip install requests
pip install requests
match_unsubscribe_websocket.py
import requests
project_key = 'YOUR_PROJ_KEY'
token = 'YOUR_ACCESS_TOKEN'
match_key = 'ausind_2020_odi01'
url = "https://api.sports.roanuz.com/v5/cricket/{}/match/{}/unsubscribe/".format(project_key,match_key)
headers = {
'rs-token': token
}
payload = {
'method': "web_socket"
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
import requests
project_key = 'YOUR_PROJ_KEY'
token = 'YOUR_ACCESS_TOKEN'
match_key = 'ausind_2020_odi01'
url = "https://api.sports.roanuz.com/v5/cricket/{}/match/{}/unsubscribe/".format(project_key,match_key)
headers = {
'rs-token': token
}
payload = {
'method': "web_socket"
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())