SydneyfunnelAllinOne
SydneyfunnelAllinOne

Reputation: 51

How to using eventSource on nodejs

I am new on nodejs

ı have a project for server sent events.

I am trying get datas with server sent events on console.

this is my code:

   var source = new EventSource('https://sse.now.sh');
source.onmessage = function(e) {
  console.log(e.data)
};

but when i try start project with node file.js :

ReferenceError: EventSource is not defined
    at Object.<anonymous> (c:\Users\SYDNEY\Desktop\server-sent-events-demo\src\client\js\app.js:7:5)
    at Module._compile (internal/modules/cjs/loader.js:953:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11

its just simple but i couldnt do anyting. should ı download anything about that ? bcz this script working on jsbin(website)

Upvotes: 1

Views: 8895

Answers (1)

SydneyfunnelAllinOne
SydneyfunnelAllinOne

Reputation: 51

I solved this problem with add top of codes

 var EventSource = require('eventsource')
 var source = new EventSource('https://sse.now.sh');
 source.onmessage = function(e) {
      console.log(e.data)
 };

Upvotes: 1

Related Questions