Search code examples
node.jsexpresssocket.iowso2wso2-cep

How to Connect WSO2CEP with Socket.io?


I want to create an Event Publisher that connect via Websocket. When I try to connect it with my simple socket io server, the url is

 ws://localhost:3000/socket.io/

It didn't receive the stream..

I've set the inline format for the stream like this :

42["input-message",{"LAT":{{latitude}},"LON":{{longitude}}}]

Solution

  • Because I cannot directly connect to the socket.io, thus I created a simple websocket that act as a middleware that sending the input from WSO2CEP into the socket.io

    var io = require('socket.io').listen(server);
    io.set('origins', '*:*');
    
    var WebSocketServer = require('ws').Server, 
       wss = new WebSocketServer({ port: 8087 })
    
    //wss sending every message that it received to the socket.io
    wss.on('connection', function connection(ws) {
      console.log('a WSO2CEP-PUBLISHER is connected');
      ws.on('message', function incoming(message) {
        console.log('received: %s', message);
        io.emit('input-message', JSON.parse(message));
      });
    });
    

    notice that, the data that come from event publisher is string formatted, so if needed to send it as a JSON object, than use JSON.parse() function.