# Can I use \_msgid for naming temporary file

**URL:** https://discourse.nodered.org/t/can-i-use-msgid-for-naming-temporary-file/14428
**Category:** General
**Created:** [17 August 2019 17:24 UTC](https://discourse.nodered.org/t/can-i-use-msgid-for-naming-temporary-file/14428 "2019-08-17T17:24:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ianmacs](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ianmacs/32/30104_2.png) [@ianmacs](https://discourse.nodered.org/u/ianmacs)
#### Post date: [17 August 2019 17:24 UTC](https://discourse.nodered.org/t/can-i-use-msgid-for-naming-temporary-file/14428/1 "2019-08-17T17:24:19Z")

</div>

I want to create a temporary file with a unique name alongside an execution path in a node-red flow. I know that this would be safe:

- Node1 - exec, calls `mktemp`, which creates a temporary file with a unique name
- Node2, fills temporary file with content
- Node3 - exec, calls executable which requires the content present in tempfile
- Node4, deletes temporary file again

And in-between some change nodes to shuffle the data between fields as needed.

Now I'm considering to replace the call to `mktemp` by using a temporary file named `"/tmp/nodered-" + msg._msgid`. Is `_msgid` unique enough provided I take care in my flow that the same original message cannot be routed to my Node1-Node4 sequence through multiple paths?

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [17 August 2019 18:02 UTC](https://discourse.nodered.org/t/can-i-use-msgid-for-naming-temporary-file/14428/2 "2019-08-17T18:02:36Z")

</div>

Yes, it only contains two hex strings joined with a dot as far as I know. That should be valid on any filing system.

---

<div class="post-metadata">

### Author: ![ianmacs](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ianmacs/32/30104_2.png) [@ianmacs](https://discourse.nodered.org/u/ianmacs)
#### Post date: [17 August 2019 18:12 UTC](https://discourse.nodered.org/t/can-i-use-msgid-for-naming-temporary-file/14428/3 "2019-08-17T18:12:54Z")

</div>

Is it unique enough? How is it created?

It would be unique enough for me if each \_msgid is the result of running a good random number generator, or if it is some combination of a datetime and an atomic counter (to differentiate between multiple messages created in the same microsecond or similar).

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [17 August 2019 18:15 UTC](https://discourse.nodered.org/t/can-i-use-msgid-for-naming-temporary-file/14428/4 "2019-08-17T18:15:57Z")

</div>

It is a random number expressed in hex.

The only thing to watch out for is any node that (improperly) creates a new msg object rather then reuse the msg it received. That could lead to a new msgid being generated.

But if for the particular set of nodes you want to use, you see msgid is stable from end to end, then go for it.

Alternatively, add your own random property at the start that you have complete control over.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [17 August 2019 19:11 UTC](https://discourse.nodered.org/t/can-i-use-msgid-for-naming-temporary-file/14428/5 "2019-08-17T19:11:28Z")

</div>

Perhaps you could also use this?

> **[node-red-contrib-uuid](https://flows.nodered.org/node/node-red-contrib-uuid)**
>
> A Node-RED Node for simple, fast generation of RFC4122 UUIDS

Though it produces a rather longer unique string like:

```
f81d4fae-7dec-11d0-a765-00a0c91e6bf6

```

[https://tools.ietf.org/html/rfc4122](https://tools.ietf.org/html/rfc4122)

---

<div class="post-metadata">

### Author: ![ianmacs](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ianmacs/32/30104_2.png) [@ianmacs](https://discourse.nodered.org/u/ianmacs)
#### Post date: [12 October 2019 20:24 UTC](https://discourse.nodered.org/t/can-i-use-msgid-for-naming-temporary-file/14428/6 "2019-10-12T20:24:05Z")

</div>

For the record, I had marked knolleary's reply as the solution because it answered my question and that's what I used in my first solution.

Later, I switched to using [node-red-contrib-uuid](https://flows.nodered.org/node/node-red-contrib-uuid) as suggested by [TotallyInformation](https://discourse.nodered.org/u/TotallyInformation) just to be on the safe side.
