What is cellophane.io ?
Cellophane.io, like Elephan.io, provides a socket.io client fully written in C that should be usable for lightweight solutions.
It is a light and easy to use library that aims to bring some real-time functionality to a C application through socket.io and websockets for actions that could not be done in full javascript,such as in embedded systems:
- Push Notifications
- IoT and Web Integration
- and more..
Why cellophane.io ?
Cellophane.io was started based on Elephan.io code, so at first time it was a C version of it. So playing a little bit with C and Elephant words we get:
C + Elephan.io = Cellophane.io
Requirements
For cellophane.io magic to operate, the following dependencies must be fulfilled :
- Socket.IO 0.8+
- cURL 7.19.6+
- NodeJS 0.6.5+
Close connection when finished (recommended)
The classic connection. It will allow you to connect to your socket.io instance and then emits something to it.
It will then disconnect at the end of the script. We recommend using this for the time being.
C server side:
#include <stdio.h>
#include <stdlib.h>
#include <socket_io.h>
int main()
{
WsClient io_client;
ws_client_new(&io_client,"http://", "localhost", 8000, "socket.io", 1, 0, 1, 1);
ws_client_emit(&io_client,"login", "foo","");
ws_client_close(&io_client);
return 0;
}
Socket.io server side:
var io = require('socket.io').listen(8000);
io.sockets.on('connection', function (socket) {
console.log('user connected!');
socket.on('foo', function (data) {
console.log('here we are in action event and data is: ' + data);
});
});
Making a persistent connection (not recommended yet)
For a persistent connection, here is an example of how to initiate the connection and emit an Hello World message.
Once the message is sent, the connection will be kept with the socket.io server. It is still not recommended to use this at the time,
as this feature is being at an early stage of development.
#include <stdio.h>
#include <stdlib.h>
#include <socket_io.h>
void notification_event_callback(char * message){
printf("Notification Data: %s\n",message);
}
int main()
{
WsClient io_client;
ws_client_io(&io_client,"http://", "localhost", 8000);
ws_client_on(&io_client,"notification", notification_event_callback);
ws_client_emit(&io_client,"login", "foo","");
return 0;
}
Future Features :
- More user-friendly methods to communicate with socket.io
- Better callback management
More to come... Feel free to submit your features requests on our Github issues page !