Networked Realities

API Documentation


Link to CDN ...

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>

... then set up socket.io

// replace your-name with your name!
const socket = io('https://api.networkedrealiti.es/your-name').connect();

Send and receive any data to other users that are connected to the server.

// socket.emit() sends data to the server ...
socket.emit('broadcast', 'hi');
// ... and socket.on() invokes a callback
// when information is received
socket.on('broadcast', (result, data) => {
    console.log(result, data);
});

Save an object to the database using the Insert channel.

socket.emit('insert', {cool: 'stuff'});
// optionally, you can access the returned object
// by passing a callback as a third argument
socket.emit('insert', {cool: 'stuff'}, data => {
    // you will notice that the server has added
    // two keys to your object: _id and _created
    // corresponding to the entry's unique I.D.
    // and the time stamp it was created
    console.log(data);
});
// Save multiple db entries as an array of objects
socket.emit('insert', [
    {cool: 'stuff'},
    {cool: 'things'},
    {rad: 'items'}
]);

Other users can also receive what was saved.

// call socket.on() so other users can
// receive the saved data
socket.on('insert', data => {
    console.log(data);
});

Retrieve information from the database using the find channel. by either sending an empty object to retrieve everything from the db, or an object with the values you wish to see.

// sending an empty object will
socket.emit('find', {}, data => {
    // the 'find' channel responds with an array.
    // use a loop to cycle through its values
    for(let i=0; i<data.length; i++){
        console.log(data[i]);
    }
});

Delete entries in the database by passing a query object containing the values you wish to match.

// just like the 'find' channel, the 'delete' channel
// takes an object as it's second argument
socket.emit('delete', {_id: 'a1a1a1a1a1a1a1a1a1a1a1a1'});
// you can access the db's deletion confirmation
// in a callback. The optional second argument is the sent query. 
socket.emit('delete', {cool: 'stuff'}, data => {
    console.log(result, query);
});

The delete channel also broadcasts the query parameter to other connected users.

socket.on('delete', query => {
    console.log(query);
});