# How to handle javascript events?

**URL:** <https://discourse.nodered.org/t/how-to-handle-javascript-events/85020>\
**Category:** General\
**Created:** [29 January 2024 13:23 UTC](https://discourse.nodered.org/t/how-to-handle-javascript-events/85020 "2024-01-29T13:23:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![protonmw](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@protonmw](https://discourse.nodered.org/u/protonmw)\
**Post date:** [29 January 2024 13:23 UTC](https://discourse.nodered.org/t/how-to-handle-javascript-events/85020/1 "2024-01-29T13:23:59Z")

</div>

Hi experts!

I want to include a CAN hardware interface into NR.  
How can I handle the events of the module?

> JavaScript Events  
> The module emits the following events:
> 
> open when the serial port is successfully opened  
> error if an error occurs  
> data when an incoming CAN bus frame is received  
> write when an outgoing CAN bus frame is sent to the device (the event is emitted before the frame is actually sent on the bus)  
> close when the port is closed

full documentation: [https://www.npmjs.com/package/@csllc/cs-pcan-usb](https://www.npmjs.com/package/@csllc/cs-pcan-usb)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 January 2024 13:41 UTC](https://discourse.nodered.org/t/how-to-handle-javascript-events/85020/2 "2024-01-29T13:41:53Z")

</div>

Any reason you are not using one of the pre-built node-red [canbus nodes](https://flows.nodered.org/search?term=canbus)?

---

<div class="post-metadata">

**Author:** ![protonmw](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@protonmw](https://discourse.nodered.org/u/protonmw)\
**Post date:** [29 January 2024 13:44 UTC](https://discourse.nodered.org/t/how-to-handle-javascript-events/85020/3 "2024-01-29T13:44:16Z")

</div>

Because I´m in a industrial environment and my company uses this interfaces.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 January 2024 13:49 UTC](https://discourse.nodered.org/t/how-to-handle-javascript-events/85020/4 "2024-01-29T13:49:50Z")

</div>

> [@protonmw](#):
>
> I´m in a industrial environment and my company uses this interfaces

um, ok, fine your company uses canbus interfaces. Great.

Perhaps my question was unclear. Let me ask a different way...

Is it not possible to use one of the existing pre-packaged canbus nodes that are already designed and working for Node-RED and dont require you to use a non packaged npm module?

---

<div class="post-metadata">

**Author:** ![protonmw](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@protonmw](https://discourse.nodered.org/u/protonmw)\
**Post date:** [29 January 2024 13:55 UTC](https://discourse.nodered.org/t/how-to-handle-javascript-events/85020/5 "2024-01-29T13:55:25Z")

</div>

all your linked nodes are for special interfaces (socketcan[linux only, we use windows] or special virtual magic interface)

---

<div class="post-metadata">

**Author:** ![protonmw](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@protonmw](https://discourse.nodered.org/u/protonmw)\
**Post date:** [5 February 2024 11:53 UTC](https://discourse.nodered.org/t/how-to-handle-javascript-events/85020/6 "2024-02-05T11:53:40Z")

</div>

@knolleary do you have any suggestions on how to handle javascript events?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 February 2024 12:32 UTC](https://discourse.nodered.org/t/how-to-handle-javascript-events/85020/7 "2024-02-05T12:32:20Z")

</div>

Assuming for 1 second you are loading this lib in a function node, you handle events in a node-red function exactly the same as you would in any nodejs application

example

```auto

can.on('some-event', function(data) {
  // do something with data e.g. send it out to next node
  node.send(topic: 'some-event', {payload: data})
})

```

for more information read the [function node docs](https://nodered.org/docs/user-guide/writing-functions)

---

<div class="post-metadata">

**Author:** ![protonmw](https://avatars.discourse-cdn.com/v4/letter/p/b3f665/32.png) [@protonmw](https://discourse.nodered.org/u/protonmw)\
**Post date:** [5 February 2024 13:18 UTC](https://discourse.nodered.org/t/how-to-handle-javascript-events/85020/8 "2024-02-05T13:18:19Z")

</div>

This works, great!  
I have no idea how an nodejs application works... 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [19 February 2024 13:19 UTC](https://discourse.nodered.org/t/how-to-handle-javascript-events/85020/9 "2024-02-19T13:19:12Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
